简体   繁体   English

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

[英]$http.post Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers error

I'm working on a app using Ionic Framework. 我正在使用Ionic Framework开发应用程序。

On the backend i wrote a Flask Application for api which looks like below: 在后端我为api写了一个Flask应用程序,如下所示:

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

I got errors while posting json to api. 将json发布到api时出错了。

var headers = {
                    'Access-Control-Allow-Origin' : '*',
                    'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS',
                    'Accept': 'application/json'
                };

                $http({
                    method: "POST",
                    headers: headers,
                    url: url+ '/api/saverez',
                    data: $scope.form
                }).success(function (result) 
                    console.log(result);
                }).error(function (data, status, headers, config) {
                    console.log(data);
                    console.log(status);
                    console.log(headers);
                    console.log(config);
                });

So this gives me the error: 所以这给了我错误:

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

I googled it and then i found this snippet: 我用Google搜索,然后我找到了这个片段:

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

I also added headers to my nginx conf like below: 我还在我的nginx conf中添加了标题,如下所示:

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

Tried everything in that documentation and also evertyhing i found on google but sadly it didn't do any good. 尝试了那些文档中的所有内容以及我在google上发现的evertyhing但遗憾的是它没有任何好处。

How can i set the right headers for all origins ? 如何为所有来源设置正确的标题? I also use google pagespeed does it can cause this issue ? 我也用google pagespeed它会导致这个问题吗?

Thanks in advance. 提前致谢。

--- EDIT --- Chrome network output ---编辑--- Chrome网络输出

Remote Address:myip
Request URL:http://myurl/api/saverez
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:access-control-allow-origin, accept, access-control-allow-methods,          content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:myurl
Origin:http://192.168.1.46:8100
Pragma:no-cache
Referer:http://192.168.1.46:8100/
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
Response Headersview source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:*
Access-Control-Allow-Methods:*
Access-Control-Allow-Origin:*
Access-Control-Max-Age:21600
Allow:POST, OPTIONS
Content-Length:0
Content-Type:text/html; charset=utf-8
Date:Thu, 28 Aug 2014 13:26:11 GMT
Server:nginx/1.6.0

In my app module config section I have the following: 在我的app模块配置部分,我有以下内容:

angular.module('starterapp', ['ionic'])
  .config(function ($stateProvider, $httpProvider, $urlRouterProvider) {

    // We need to setup some parameters for http requests
    // These three lines are all you need for CORS support
    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
  }

That is all you need to have to make all the HTTP requests work with CORS. 这就是让所有HTTP请求与CORS一起工作所需的全部内容。 This of course assumes you have made your backend. 这当然假设你已经做了你的后端。

You adding those additional headers would not be allowed according the w3c specification for XMLHTTPRequest as they may only be added by the host browser. 根据XMLHTTPRequest的w3c规范,不允许添加这些附加标头,因为它们可能只能由主机浏览器添加。

PaulT's answer got me very close to solving this problem for myself, but I also had to explicitly add the Content-Type for my post operations. PaulT的回答让我非常接近为自己解决这个问题,但我还必须为我的帖子操作明确添加Content-Type。

$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';

Hope this helps. 希望这可以帮助。

I got it working by just adding JSONP as the method 我通过添加JSONP作为方法来实现它

$resource( ' http://maps.google.com/maps/api/geocode/json?address=:address&sensor=false ', {}, { get: { method: 'JSONP', } }); $ resource(' http://maps.google.com/maps/api/geocode/json?address=:address&sensor=false ',{},{get:{method:'JSONP',}});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请求标头字段Access-Control-Allow-Origin在预检响应中不允许使用Access-Control-Allow-Origin - Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response $ http.post-请求标头字段Access-Control-Allow-Headers不允许授权 - $http.post - Request header field Authorization is not allowed by Access-Control-Allow-Headers Angular JS。 飞行前响应中Access-Control-Allow-Headers不允许请求标头字段Access-Control-Allow-Origin - Angular JS. Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response angularjs和PHP:请求头字段Access-Control-Allow-Origin在预检响应中不允许使用Access-Control-Allow-Origin - angularjs and PHP : Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response angularJS $ http.Post无法正常工作:无法加载资源:Access-Control-Allow-Headers不允许请求标头字段0 - angularJS $http.Post not working: Failed to load resource: Request header field 0 is not allowed by Access-Control-Allow-Headers 具有$ http的Access-Control-Allow-Header不允许使用请求标头字段 - Request header field is not allowed by Access-Control-Allow-Headers with $http Access-Control-Allow-Headers 不允许请求头字段 Access-Control-Allow-Headers - Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers 角$ http.post并且没有'Access-Control-Allow-Origin'标头 - Angular $http.post and No 'Access-Control-Allow-Origin' header angularjs $ http.post上的错误access-control-allow-origin - Error access-control-allow-origin on angularjs $http.post 请求标头字段不允许Access-Control-Allow-Origin - Request header field Access-Control-Allow-Origin is not allowed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM