我正在使用nginx的slim php框架,我也启用了CORSs。现在,当我从其他域向我的 Rest API 发出请求时,首先调用选项方法,然后使用实际的发布数据调用 post 方法。开机自检的响应为 200,但没有来自 API 的数据。
但是当我在同一域中尝试相同的方法时,一切都很完美。
加工
$.ajax({
url: "http://172.19.4.130/oam_restapi/restapi/loginpost",
type: 'post',
dataType: 'json',
contentType: "application/json; charset=utf-8",
crossDomain:true,
data : JSON.stringify({
"username" : "admin",
"password" : "admin!23B",
"domain" : "local"
}),
success: function(res) {
console.log(res);
}
});
当这个 ajax 调用像 172.19.4.130/makeajax.html 一样进行时
不工作
当像 localhost/makeajax.html 一样进行相同的调用时
我了解 CORS ,之前与 APACHE 合作也是如此
Nginx 配置
location /oam_restapi/restapi/ {
satisfy any;
add_header Access-Control-Allow-Origin *;
root /html/oam_restapi/restapi/;
index index.php;
try_files $uri /oam_restapi/restapi/index.php;
}
您需要在
后端的响应中包含Access-Control-Allow-Origin
标头。
看看 https://github.com/palanik/CorsSlim