为什么服务器返回错误405添加授权标头时不允许方法?

时间:2016-10-28 03:53:14

标签: php angularjs slim http-status-code-405

我有一个简单的API https://hoply-hoply.c9users.io/api/public/categories 以下是我的测试 角度代码没有标题运作良好http://codepen.io/phanvanlinh94vn/pen/pEmAPZ

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $http.get("https://hoply-hoply.c9users.io/api/public/categories")
  .then(
    function(response) {
      console.log("success"); console.log(response);
    },
    function (error) {
      console.log("error"); console.log(error);
    });
});

Angular Code WITH 标头返回错误405(http://codepen.io/phanvanlinh94vn/pen/JRbvZd

var app = angular.module('myApp', []);
var config = {headers:  {
        'Authorization': 'Bearer d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==',
        'Accept': 'application/json;odata=verbose',
        "X-Testing" : "testing"
 }};
app.controller('myCtrl', function($scope, $http) {
  $http.get("http://hoply-hoply.c9users.io/test_category.php", config)
  .then(
    function(response) {
      console.log("success"); console.log(response);
    },
    function (error) {
      console.log("error"); console.log(error);
    });
});

enter image description here

这是我的PHP(带有Slim框架)代码,用于句柄返回错误405

$container['notAllowedHandler'] = function ($c) {
    return function ($request, $response, $methods) use ($c) {
        return $c['response']
            ->withStatus(405)
            ->withHeader('Allow', implode(', ', $methods))
            ->withHeader('Content-type', 'text/html')
            ->write('Method must be one of: ' . implode(', ', $methods));
    };
};

现在的问题是当我删除用于处理405的PHP块代码时。请求API(没有标题和标题)正常工作
我不知道为什么会这样。我还是想处理405错误。 我该如何解决我的问题?

非常感谢任何帮助或建议。

0 个答案:

没有答案