$ http请求上的Angular 405错误

时间:2014-09-26 02:52:41

标签: javascript php angularjs fuelphp

这个问题与我发布的这个问题有关。

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

1 个答案:

答案 0 :(得分:0)

来自FuelPHP方面:

假设您使用Controller_Rest创建此功能:

如果它不能将请求与控制器中的任何方法匹配,则返回405状态。

再次假设使用类似http://event.chart.local/api/vendor/events.json的链接,您的控制器是Controller_Api_Vendor(或名称空间变体),方法是" events"?

在这种情况下,它将查找options_events()或action_events()。如果其中一个存在且可调用,则405不会由Fuel生成。