2014-12-26 35 views
3

我有一個問題試圖對我的休息PHP服務器進行$ http調用。 我正在從客戶端到後端進行跨域調用。AngularJS,PHP Restful Cors問題

從我的角度應用程序中,這是$ 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 

但是實際的POST請求失敗:

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. 

這些請求和響應頭:

**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 

我顯然缺少在關閉後請求的響應一些頭。

但是,它的失敗問題在預檢請求中起作用。我已經嘗試在其他服務器的index.php中添加一些頭文件,但請求並沒有到達那裏,並且更早被卡住(我猜測加載.htaccess的時候)。

我在這裏錯過了什麼?

回答

8

了幾個小時,我終於發現了這件事之後。

我的.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羅丹了幾乎是正確的。不應將「添加」更改爲「設置」,而應將其設置爲「始終設置」。

site幫了我:

關於AngularJS,我可以證實,不需要其他設置CORS工作。 所以Angular的$ http並不需要用頭文件等進行更改,默認設置應該與上面提到的.htaccess條目一起使用。

+0

YEAH thx非常多:) – habamedia

1

嘗試使用set代替add在你的.htaccess

Header set Access-Control-Allow-Origin "*"

查看更多here

+0

謝謝,我試過了,但沒有運氣。更改添加設置,重新啓動apache,但問題仍然存在。預檢請求正常,請求不是。 –