gpt4 book ai didi

angularjs - $http.post Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers 错误

转载 作者:行者123 更新时间:2023-12-03 16:03:32 31 4
gpt4 key购买 nike

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

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

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

我在将 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);
});

所以这给了我错误:

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 中添加了 header ,如下所示:

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

尝试了该文档中的所有内容以及我在谷歌上找到的所有内容,但遗憾的是它没有任何用处。

如何为所有来源设置正确的标题?我也使用 google pagespeed 它会导致这个问题吗?

提前致谢。

--- 编辑 ---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

最佳答案

在我的应用程序模块配置部分,我有以下内容:

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'];
}

这就是让所有 HTTP 请求都使用 CORS 所需的全部内容。这当然假设您已经创建了后端。

根据 XMLHTTPRequest 的 w3c 规范,您添加这些额外的 header 是不允许的,因为它们只能由主机浏览器添加。

关于angularjs - $http.post Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25550337/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com