angularjs-$http.post请求标头字段Access-Control-Allow-Headers错误不允许Access-Control-Allow-Origin

前端之家收集整理的这篇文章主要介绍了angularjs-$http.post请求标头字段Access-Control-Allow-Headers错误不允许Access-Control-Allow-Origin 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用Ionic Framework开发应用程序.

在后端,我为api编写了Flask应用程序,如下所示:

  1. @API.route("/saverez",methods=["POST","OPTIONS"])
  2. @crossdomain(origin='*',headers="*",methods="*")
  3. @render_api
  4. def saver():
  5. .....

将json发布到api时出现错误.

  1. var headers = {
  2. 'Access-Control-Allow-Origin' : '*','Access-Control-Allow-Methods' : 'POST,GET,OPTIONS','Accept': 'application/json'
  3. };
  4. $http({
  5. method: "POST",headers: headers,url: url+ '/api/saverez',data: $scope.form
  6. }).success(function (result)
  7. console.log(result);
  8. }).error(function (data,status,headers,config) {
  9. console.log(data);
  10. console.log(status);
  11. console.log(headers);
  12. console.log(config);
  13. });

所以这给了我错误

  1. XMLHttpRequest cannot load http://myurl/api/saverez. Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers.

我用谷歌搜索,然后发现以下代码片段:

http://flask.pocoo.org/snippets/56/

我还将标题添加到我的Nginx conf中,如下所示:

  1. location ~* \.(eot|ttf|woff)${
  2. add_header Access-Control-Allow-Origin *;
  3. }

尝试了该文档中的所有内容以及我在Google上发现的所有东西,但可惜它没有任何好处.

如何为所有来源设置正确的标题?我也使用google pagespeed是否会导致此问题?

提前致谢.

-编辑-
Chrome网络输出

  1. Remote Address:myip
  2. Request URL:http://myurl/api/saverez
  3. Request Method:OPTIONS
  4. Status Code:200 OK
  5. Request Headersview source
  6. Accept:*/*
  7. Accept-Encoding:gzip,deflate,sdch
  8. Accept-Language:en-US,en;q=0.8
  9. Access-Control-Request-Headers:access-control-allow-origin,accept,access-control-allow-methods,content-type
  10. Access-Control-Request-Method:POST
  11. Cache-Control:no-cache
  12. Connection:keep-alive
  13. Host:myurl
  14. Origin:http://192.168.1.46:8100
  15. Pragma:no-cache
  16. Referer:http://192.168.1.46:8100/
  17. User-Agent:Mozilla/5.0 (iPhone; cpu iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML,like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53
  18. Response Headersview source
  19. Access-Control-Allow-Credentials:true
  20. Access-Control-Allow-Headers:*
  21. Access-Control-Allow-Methods:*
  22. Access-Control-Allow-Origin:*
  23. Access-Control-Max-Age:21600
  24. Allow:POST,OPTIONS
  25. Content-Length:0
  26. Content-Type:text/html; charset=utf-8
  27. Date:Thu,28 Aug 2014 13:26:11 GMT
  28. Server:Nginx/1.6.0
最佳答案
在我的应用程序模块配置部分,我有以下内容

  1. angular.module('starterapp',['ionic'])
  2. .config(function ($stateProvider,$httpProvider,$urlRouterProvider) {
  3. // We need to setup some parameters for http requests
  4. // These three lines are all you need for CORS support
  5. $httpProvider.defaults.useXDomain = true;
  6. $httpProvider.defaults.withCredentials = true;
  7. delete $httpProvider.defaults.headers.common['X-Requested-With'];
  8. }

这就是使所有HTTP请求与CORS一起使用所需要的.当然,这是假设您已经建立了后端.

根据XMLHTTPRequest的w3c规范,您不能添加这些其他标头,因为它们只能由主机浏览器添加.

猜你在找的Nginx相关文章