Javascript CORS标头包含信息,但Angular js$http.get不起作用

Javascript CORS标头包含信息,但Angular js$http.get不起作用,javascript,angularjs,ajax,cors,Javascript,Angularjs,Ajax,Cors,我使用angular js使用rest服务。RESTAPI确实返回所需的头,但我得到 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. (Reas

我使用angular js使用rest服务。RESTAPI确实返回所需的头,但我得到

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;

         });

我是否做错了什么,或者问题在标题中。

当我阅读您的问题时,我也遇到了这个问题,这个问题可以通过创建代理服务器从服务器端和客户端解决。在服务器端,您需要允许系统的ip

据我所知,他们通常有三种解决方案

1) 。 示例:正如我在NodeJs(API)中创建web服务时所做的那样:

然后您也可以运行get和post请求

2) 。您还可以创建代理服务器来处理该post和put REUEST,以便与该api的通信保持不变

3) 。您可以在chrome浏览器中安装CORS插件并启用它,还可以向服务器端查询。

您已将
授权
作为允许的标题。这是不正确的,相反,您需要添加允许此标题的
访问控制允许凭据


此单独设置控制浏览器发送cookie以及
授权
标题

我找到了解决方案。它缺少飞行前的选择。我不得不在我的后端添加这些。现在它工作正常。

您没有显示您引用的响应标题的状态代码,但浏览器说它是401(意味着未经授权),所以您需要在服务器上解决这个问题。我的api在rest客户端工作正常,但当我尝试从angular js应用程序使用它时,我得到了这个结果。服务器通过移动或rest API正常。服务器正在返回401。请注意,您发布的代码执行的是
POST
,而不是
GET
“api在rest客户端运行良好,但当我尝试从angular js应用程序使用它时,我得到了这个”。所以,您需要做的就是比较这两种情况下的头。非常简单,“我的api在rest客户端上运行得很好,但当我尝试在angular js应用程序中使用它时,我得到了”-很明显!如果您不使用常规的web应用程序,您就不会发出浏览器说您响应不正确的飞行前请求!你的答案无效。2.我已经添加了这些标题,请参见我的标题2。代理服务器不是一个选项,因为它会破坏性能,然后是3。我的api将向所有人公开,因此我不能要求所有用户添加crome插件。是的,我试过了,但它不起作用:(@HermantAlso值得一试,用一个特定的值来设置allow origin header,而不是*,我以前在这方面遇到过问题。您需要获取浏览器正在发送的origin header并返回它
res.setHeader('Access-Control-Allow-Origin', 'http://hostname.com');
// Request methods you wish to allow
// You can write * also for allowing to access that url from all systems which is not done usually for security purposes
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);