我正在通过ajax调用向我自己开发的Slim Api发送放置请求。它仅返回404错误。我已经阅读了有关启用CORS策略的Slim文档,并已实现,但未成功。现在,我只需将所需的标头添加到api中即可启用。 简单的请求工作正常。我想知道您是否可以澄清问题所在以及如何解决?
Slim应用程序中的以下功能可启用CORS
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE, PATCH");
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header("Access-Control-Allow-Headers: *");
}
以下是Ajax Put请求
$.ajax({
url: url,
type: 'PUT',
data: data,
dataType: "json",
crossDomain: true,
headers: {
"accept": "application/json",
"Access-Control-Allow-Origin":"http://test.com"
},
success:function(data){
fn(data);
},
error: function(xhr){
console.log(xhr);
}
});