簡體   English   中英

盡管啟用了CORS,但是$ http angular無法正常工作

[英]$http angular doesn't work despite CORS enabled

在深入探討角度之前,我先在服務器上進行了干凈測試,以檢查是否啟用了CORS。 使用此腳本。

    var xhr     = new XMLHttpRequest();
    var param   = 'name=mrA';
    xhr.open('POST','http://api.otamarket.local/api/cors.json',true);

    //Send the proper header information along with the request
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xhr.onreadystatechange = function () {
      if (this.status == 200 && this.readyState == 4) {
        console.log('response: ' + this.responseText);
      }
    };
    xhr.send(param);

如您所見,它工作正常。

在此處輸入圖片說明

但是當我通過$ http使用angular時

var http = "//api.otamarket.local/api/cors.json";
return $http.post(http,credentials);

我得到這個錯誤。

在此處輸入圖片說明

這是標題響應:

Remote Address:127.0.0.1:80
Request URL:http://api.otamarket.local/api/cors.json
Request Method:OPTIONS
Status Code:405 Method Not Allowed

Request Headers view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:api.otamarket.local
Origin:http://otakuket.local
Referer:http://otakuket.local/
User-Agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36

Response Headersview source
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Length:0
Content-Type:text/html
Date:Fri, 28 Nov 2014 03:47:31 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11
X-Powered-By:PHP/5.5.11

這是我啟用CORS的地方

<VirtualHost *:80>
    DocumentRoot "C:\Users\aldri_000\SkyDrive\Program  Experiment\websites\api.otamarket.local\public"
    ServerName api.otamarket.local
    Header set Access-Control-Allow-Origin "*"
<Directory "C:\Users\aldri_000\SkyDrive\Program Experiment\websites\api.otamarket.local">
    Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order Deny,Allow
        Allow from all
        Require all granted
</Directory>
</VirtualHost>

這是端點:

public function post_cors()
{
   $rsp            = array();
   $rsp['rsp']     = 'CORS Work';
   return $this->response($rsp);
}

如您所見,我已經在apache服務器的虛擬主機中啟用了CORS。 它通過XHR運行,為什么在這里不起作用我感到困惑。

謝謝。

我看到,使用$ http調用時content-type設置不正確。 嘗試像這樣在您的$ http調用中添加headers屬性,

$http({
method: 'POST',
url: url,
data: $.param({fkey: "key"}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}

})

您的錯誤消息明確指出該錯誤與CORS不相關。

返回405,這意味着request方法與服務器上定義的方法不匹配。

如您在標題中所看到的,您的請求方法是OPTIONS,而我假設您的服務器方法是為POST設計的。

交叉檢查是否編寫了任何HTTP攔截器,或者是否有一段代碼正在更改請求標頭。

另外,由於要檢索數據,所以為什么不使用Get Request ..

試試這個代碼片段:

$http({method: 'GET', url: '//api.otamarket.local/api/cors.json'}).
        success(function(data, status) {
          $scope.status = status;
          $scope.data = data;
        }).
        error(function(data, status) {
          $scope.data = data || "Request failed";
          $scope.status = status;
      });

Angular Preflights POST / DELETE / GET / PUT請求首先帶有OPTIONS請求。 在服務器成功響應並指出服務器允許的方法后,angular然后發出PUT / POST / XXX請求。 您的服務器從Angular接收OPTIONS時未響應允許的請求類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM