Slim Framework v3 - PUT 路由问题
Slim Framework v3 - Problems with PUT routes
自从我升级到 Slim v3
后,我所有的 GET
和 POST
路由都运行良好,但 PUT
路由却不行。
我有一个 class 在那里我做这样的事情:
$this->slimObj->put('/path/{ID}', array($this, 'method'));
function method ( $request, $response, $args ) {
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withHeader('Access-Control-Allow-Origin', '*');
$ID = $args['ID'];
// ...
return $response;
}
我使用 Chrome 48
调试我的 Cordova
应用程序,在 PUT
调用之后我收到此错误:
XMLHttpRequest cannot load
http://example.com/file.php/path/149. No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:4400' is therefore not allowed access. The response had HTTP status code 400.
我对 GET
和 POST
请求使用了类似的回调,它们工作正常,我不明白为什么它不适用于 PUT
。
从 curl
调用它时有效:
curl -X PUT -H 'Content-Type: application/json' -d '{"data": 5 }' http://example.com/file.php/path/149
我使用 ajax
:
从 JS
调用 Slim API 函数
$.ajax({
type: 'PUT',
url: 'http://example.com/file.php/path/149',
dataType: "json",
data: {
"data": 5
},
success: function(result) {
// ...
},
error: function() {
// Always getting error
}
});
我怎样才能让它在 Chrome 48
上运行?
我不太确定,但我认为以前的 Chrome
版本一切正常。
谢谢
我刚刚通过添加这个解决了:
$this->slimObj->add(function ($request, $response, $next) {
$response = $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
$response = $next ($request, $response);
return $response;
});
在初始化任何路由之前。
自从我升级到 Slim v3
后,我所有的 GET
和 POST
路由都运行良好,但 PUT
路由却不行。
我有一个 class 在那里我做这样的事情:
$this->slimObj->put('/path/{ID}', array($this, 'method'));
function method ( $request, $response, $args ) {
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withHeader('Access-Control-Allow-Origin', '*');
$ID = $args['ID'];
// ...
return $response;
}
我使用 Chrome 48
调试我的 Cordova
应用程序,在 PUT
调用之后我收到此错误:
XMLHttpRequest cannot load
http://example.com/file.php/path/149. No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:4400' is therefore not allowed access. The response had HTTP status code 400.
我对 GET
和 POST
请求使用了类似的回调,它们工作正常,我不明白为什么它不适用于 PUT
。
从 curl
调用它时有效:
curl -X PUT -H 'Content-Type: application/json' -d '{"data": 5 }' http://example.com/file.php/path/149
我使用 ajax
:
JS
调用 Slim API 函数
$.ajax({
type: 'PUT',
url: 'http://example.com/file.php/path/149',
dataType: "json",
data: {
"data": 5
},
success: function(result) {
// ...
},
error: function() {
// Always getting error
}
});
我怎样才能让它在 Chrome 48
上运行?
我不太确定,但我认为以前的 Chrome
版本一切正常。
谢谢
我刚刚通过添加这个解决了:
$this->slimObj->add(function ($request, $response, $next) {
$response = $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
$response = $next ($request, $response);
return $response;
});
在初始化任何路由之前。