Reputation: 591
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"];
Upvotes: 1
Views: 111
Reputation: 591
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));
});
}
Upvotes: 0
Reputation: 50767
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);
Upvotes: 0