I have this piece of jQuery code that works fine cross origin:
jQuery.ajax({
url: "http://example.appspot.com/rest/app",
type: "POST",
data: JSON.stringify({"foo":"bar"}),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (response) {
console.log("success");
},
error: function (response) {
console.log("failed");
}
});
Now I'm tring to convert this to Angular.js code without any success:
$http({
url: "http://example.appspot.com/rest/app",
dataType: "json",
method: "POST",
data: JSON.stringify({"foo":"bar"}),
headers: {
"Content-Type": "application/json; charset=utf-8"
}
}).success(function(response){
$scope.response = response;
}).error(function(error){
$scope.error = error;
});
Any help appreciated.
You may use this :
Download "angular-post-fix": "^0.1.0"
Then add 'httpPostFix' to your dependencies while declaring the angular module.
Ref : https://github.com/PabloDeGrote/angular-httppostfix
The AngularJS way of calling $http would look like:
or could be written even simpler using shortcut methods:
There are number of things to notice:
success
anderror
respectively (also please note parameters of each callback) - Deprecated in angular v1.5then
function instead.then
usage can be found hereThe above is just a quick example and some pointers, be sure to check AngularJS documentation for more: http://docs.angularjs.org/api/ng.$http
We can implement ajax request by using http service in AngularJs, which helps to read/load data from remote server.
$http service methods are listed below,
One of the Example:
http://www.drtuts.com/ajax-requests-angularjs/
you can use $.param to assign data :
look at this : AngularJS + ASP.NET Web API Cross-Domain Issue