【发布时间】:2016-04-09 11:25:48
【问题描述】:
我正在使用 Angular js 来使用休息服务。其余的 api 确实返回了所需的标头,但我得到了
Response for preflight has invalid HTTP status code 401
在 Mozilla 中我得到了
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://testurl.com:8081/v1/users. (Reason: CORS preflight channel did not succeed).
错误
API 头返回给
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, PUT, GET, OPTIONS, DELETE
Access-Control-Allow-Headers: x-requested-with, Authorization
Access-Control-Expose-Headers: x-requested-with
Access-Control-Max-Age: 3600
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
X-Application-Context: application:8081
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 05 Jan 2016 14:57:20 GMT
下面是http请求
$http({
method: 'post',
url: 'http://testurl.com/v1/users',
data : data
}).then(function successCallback(response) {
object.sucess=true;
object.massage=response;
console.log('success');
}, function errorCallback(response) {
object.sucess=true;
object.massage=response;
});
我做错了什么还是问题出在标题中。
【问题讨论】:
-
您没有显示您引用的响应标头的状态代码,但浏览器显示它是 401(表示未经授权),因此您需要在服务器上解决该问题。
-
我的 api 在 rest 客户端上运行良好,但是当我尝试从 angular js 应用程序中使用它时,我得到了这个。服务器从移动或休息 api 都可以
-
server 正在返回 401。请注意,您发布的代码是
POST,而不是GET。 -
"api 在 rest 客户端上工作正常,但是当我尝试从 angular js 应用程序中使用它时,我得到了这个"。所以你需要做的就是在这两种情况下比较标题。很简单。
-
“我的 api 在 rest 客户端上运行良好,但是当我尝试从 angular js 应用程序中使用它时,我得到了”——嗯,很明显!如果您不使用常规网络应用程序,您将不会发出浏览器说您响应不正确的预检请求!
标签: javascript angularjs ajax cors