【问题标题】:Angular 405 error on $http request$http 请求出现 Angular 405 错误
【发布时间】:2014-11-20 23:29:30
【问题描述】:

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

Allow my API to be access via AJAX

我想通过按照http://enable-cors.org/server_apache.html 上的说明添加来修复 CORS 错误。现在问题没有出现。

但这一次,我收到了一个 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

【问题讨论】:

  • 您的服务器是否允许OPTIONS 请求?尝试在回复中添加Access-Control-Allow-Methods: POST, GET, OPTIONS
  • 是的。如上面的标题信息所示。 @akonsu
  • 你确定是events.json。如何发布到 json 文件?你的意思是get 还是其他的api?
  • @psl 这是一种燃料。这意味着响应将被编码为 json,这就是 .json 在 url 中的用途。

标签: javascript php angularjs fuelphp


【解决方案1】:

从 FuelPHP 方面:

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

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

再一次,假设使用http://event.chart.local/api/vendor/events.json 之类的链接,您的控制器是 Controller_Api_Vendor(或命名空间变体),方法是“事件”?

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

【讨论】:

  • 该链接适用于正常的 curl 请求。它正在使用 Controller_Rest 并且它在正确的方法上,如 post_event 函数名称所示。
  • 那么我只能假设您的网络服务器未配置为接受这些请求
猜你喜欢
  • 2017-09-09
  • 1970-01-01
  • 2010-12-18
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
  • 2019-04-20
  • 2021-09-21
  • 2017-05-19
相关资源
最近更新 更多