2014-09-26 104 views
0

這個問題與我發佈的這個問題有關。

Allow my API to be access via AJAX

我設法解決CORS錯誤,我想加入下面就http://enable-cors.org/server_apache.html的說明。這個問題現在不出現。

但是這一次,我得到了一個405錯誤,我不知道爲什麼如此。

客戶

var appOrigin = 'http://event.chart.local/api/vendor/events.json'; 
app.factory('chartListLoadService',['$http',function($http){ 
    return { 
     fetchList: function(city){ 
      var data  = {city:city}; 
      $http({ 
       method:'post', 
       url:appOrigin, 
       data:data 
      }).success(function(data){ 
       alert(data); 
      }); 
     }, 
     testFunction:function(){ 
      alert('testing'); 
     } 
    }; 
}]); 

服務器

public function post_events() 
{ 
    $city = Input::post('city','all'); 
    $c = Model_chart::format_chart($city); 
    return $this->response($c); 
} 

頁眉信息

Remote Address:127.0.0.1:80 
Request URL:http://event.chart.local/api/vendor/events.json 
Request Method:OPTIONS 
Status Code:405 Method Not Allowed 
Request Headersview parsed 
OPTIONS /api/vendor/events.json HTTP/1.1 
Host: event.chart.local 
Connection: keep-alive 
Access-Control-Request-Method: POST 
Origin: http://app.event.chart 
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36 
Access-Control-Request-Headers: accept, content-type 
Accept: */* 
Referer: http://app.event.chart/ 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 
Response Headersview parsed 
HTTP/1.1 405 Method Not Allowed 
Date: Fri, 26 Sep 2014 02:48:33 GMT 
Server: Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11 
X-Powered-By: PHP/5.5.11 
Access-Control-Allow-Origin: http://app.event.chart 
Access-Control-Allow-Methods: POST, GET, OPTIONS 
Access-Control-Allow-Headers: Content-Type 
Keep-Alive: timeout=5, max=100 
Connection: Keep-Alive 
Transfer-Encoding: chunked 
Content-Type: text/html 
+0

你的服務器允許'OPTIONS'請求嗎?嘗試添加'Access-Control-Allow-Methods:POST,GET,OPTIONS'到響應中。 – akonsu 2014-09-26 03:02:00

+0

是的。如上面的標題信息所示。 @akonsu – 2014-09-26 03:05:55

+0

你確定它是'events.json'。你如何發佈到json文件?你是指「get」還是其他的東西? – PSL 2014-09-26 03:24:55

回答

0

從FuelPHP方面:

假設你使用Controller_Rest創建此functi onality:

如果它無法將請求與控制器中的任何方法相匹配,則返回405狀態。

再次假設您的控制器是類似於http://event.chart.local/api/vendor/events.json的鏈接,它是Controller_Api_Vendor(或名稱空間變體),並且方法是「events」?

在這種情況下,它將查找options_events()或action_events()。如果其中一個存在且可被調用,則405不是由Fuel生成的。

+0

該鏈接適用於正常捲曲請求。在那裏它使用了Controller_Rest,並且它在post_event函數名稱中指出的正確方法上。 – 2014-09-26 07:48:51

+0

然後我只能假設你的網絡服務器沒有被配置爲接受這些請求 – WanWizard 2014-09-26 16:00:11