【发布时间】:2012-08-21 07:57:49
【问题描述】:
我有这段 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;
});
任何帮助表示赞赏。
【问题讨论】:
-
不知道 angular.js 但可能 faile() 是一个错误的函数名称?
-
可能找到了解决方案stackoverflow.com/questions/12111936/…需要深入挖掘...
-
OPTIONS 请求将由浏览器发出,它对 AngularJS/您的应用程序是透明的。如果 OPTION 成功,原始请求(POST/GET/whatever)将随之而来,并且您的代码将被回调为主请求而不是 OPTION 请求。
-
可能不是 Angular 将请求方法更改为 OPTIONS。可能是您的浏览器检查它是否可以执行 CORS 请求。如果您尝试调用一个单独的域,您的浏览器将首先发出一个 OPTIONS 请求以查看是否允许。
标签: jquery ajax angularjs cross-domain angular-http