gpt4 book ai didi

php - AngularJS,PHP Restful Cors 问题

转载 作者:可可西里 更新时间:2023-11-01 00:08:54 27 4
gpt4 key购买 nike

我在尝试对我的其余 PHP 服务器进行 $http 调用时遇到问题。我正在进行从客户端到后端的跨域调用。

在我的 Angular 应用程序中,$http 服务是这样配置的:

.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('httpResponseInterceptor');
$httpProvider.interceptors.push('httpTimeStampMarker');
$httpProvider.defaults.useXDomain = true;
$httpProvider.defaults.headers.post['Access-Control-Allow-Origin'] = '*';
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
delete $httpProvider.defaults.headers.common['Content-Type, X-Requested-With'];
}])

实际的 $http.post() 是这样配置的:

    // Set the headers
var headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*'
};

return $http({
method: "POST",
url: base_url,
data: $.param(args),
headers: headers
})
.success(function(data, status) {

})
.error(function(data, status) {

});

这是服务器的.htaccess:

# Cors
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Methods"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

预检请求正常:

**Request headers:**
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
Connection:keep-alive
Host:***
Origin:***
Referer:***
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36

**Response headers**
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Methods
Access-Control-Allow-Methods:PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Allow:OPTIONS,GET,HEAD,POST
Connection:Keep-Alive
Content-Length:0
Content-Type:httpd/unix-directory
Date:Fri, 26 Dec 2014 07:12:24 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.9 (Unix) PHP/5.6.2

但是实际的发布请求失败了:

XMLHttpRequest cannot load http://***/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '***' is therefore not allowed access. The response had HTTP status code 404.

这些是请求和响应 header :

**Request headers**
Accept:*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:130
Content-Type:application/x-www-form-urlencoded
Host:***
Origin:http://***
Referer:***
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36


**Response headers**
Connection:Keep-Alive
Content-Length:198
Content-Type:text/html; charset=iso-8859-1
Date:Fri, 26 Dec 2014 07:12:24 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.4.9 (Unix) PHP/5.6.2

我显然在发布请求的响应中遗漏了一些 header 。

但是它失败的问题在预检请求中确实有效。我试过在其余服务器的 index.php 中添加一些 header ,但请求没有到达那里并且更早地卡住了(我猜 .htaccess 何时加载)。

我在这里错过了什么?

最佳答案

几个小时后我终于找到了。

我的.htaccess:

# Cors
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin"
Header always set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

@Alexey Rodin 给出的提示几乎是正确的。不要将“添加”更改为“设置”,而应将其设置为“始终设置”。

site帮助我:

关于 AngularJS,我可以确认 CORS 工作不需要其他设置。因此 Angular 的 $http 不需要用 header 等进行更改,默认设置应该适用于上述的 .htaccess 条目。

关于php - AngularJS,PHP Restful Cors 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27654135/

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