我有一段jQuery代码,它可以很好地跨源代码工作:
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");
}
});
现在,我尝试将其转换为Angular.js代码,但没有成功:
$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;
});
感谢您的帮助。
发布于 2016-09-16 22:32:09
在AngularJs中使用http服务实现ajax请求,可以方便地从远程服务器读取/加载数据。
下面列出了$http服务方法:
$http.get()
$http.post()
$http.delete()
$http.head()
$http.jsonp()
$http.patch()
$http.put()
其中一个示例:
$http.get("sample.php")
.success(function(response) {
$scope.getting = response.data; // response.data is an array
}).error(){
// Error callback will trigger
});
发布于 2017-03-30 18:08:50
您可以使用以下命令:
下载"angular-post-fix":"^0.1.0“
然后在声明angular模块时将'httpPostFix‘添加到您的依赖项中。
发布于 2014-03-12 00:31:30
您可以使用$.param来分配数据:
$http({
url: "http://example.appspot.com/rest/app",
method: "POST",
data: $.param({"foo":"bar"})
}).success(function(data, status, headers, config) {
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
https://stackoverflow.com/questions/12131659
复制相似问题