1

Iam doing a post request to a php script form my AngularJS for mobile app. when iam using this code form action="url" method="post" in html.its working fine. but when iam using ajax url request in angularjs , php server is not receiving the values.Then how can i achieve that

mycode:

var data: { jwt: '[email protected]' }

    var req={
      method : 'POST',
    url: 'http:url//'
    }

       $http(req).success(function(data){

    alert(JSON.stringify(data));

       }).error(function(data) {
        alert("failure");

    alert(JSON.stringify(data));


       });


 }

myphp code:

$id = $_POST["jwt"];

3
  • Are you getting any error messages in the JS console? Angular is not like PHP. It's quite a bit more verbose at what's going on. At first guess you might be having some problems with XHR but check that JS console... Commented Jan 21, 2016 at 17:57
  • error: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:8100' is therefore not allowed access. The response had HTTP status code 500. Commented Jan 21, 2016 at 18:02
  • So you have a classic cross-domain error. You can't run an XHR request from two pages residing on the same machine. Try uploading the code to a server, or you can disable XHR permissions in chrome which has been covered numerous times on stack, including here: stackoverflow.com/questions/22026984/… Also, as Ohgodwhy suggests below, you aren't quite using $http.post correctly. But one error at a time. Commented Jan 21, 2016 at 19:18

2 Answers 2

0

This is the incorrect way to use $http.post. It's just a wrapper for $http({}). What you actually want is either to just use $http({}) or use $http.post() accordingly:

$http.post(
    req.url,
    //your data
).then(successCallback, errorCallback);
Sign up to request clarification or add additional context in comments.

Comments

0
this is th prefect solution. i got the answer

var app=angulamodule("myapp",[])
.config(function ($httpProvider){
$httpProvider.defaults.transformRequest = function(data){
        if (data === undefined) {
            return data;
        }
        return serialize(data);
    };  
 $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
})


var serialize = function(obj, prefix) {
  var str = [];
  for(var p in obj) {
    if (obj.hasOwnProperty(p)) {
      var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
      str.push(typeof v == "object" ?
        serialize(v, k) :
        encodeURIComponent(k) + "=" + encodeURIComponent(v));
    }
  }
  return str.join("&");
};

var req={ method : 'POST', url: 'http:url//' }

   $http(req).success(function(data){

alert(JSON.stringify(data));

   }).error(function(data) {
    alert("failure");

alert(JSON.stringify(data));


   });

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.