使用nginx的Slim php框架,启用了CORS,请求状态为200但响应数据未到来

我使用的是使用nginx的瘦身框架,我也启用了CORS。 现在,当我从其他域向我的Rest API发出请求时,首先调用option方法,然后使用实际的post数据调用post方法。
对于POST的响应是200但是没有来自API的数据。</ p >

但是当我尝试相同的域时,一切都很完美。</ p>

工作</ h1>

  $。  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);

}

});
</ code> </ pre>

当这个ajax调用如172.19.4.130/makeajax.html


nnn

没有工作</ h1>

当同样的调用如同 localhost / makeajax.html </ p>

我理解CORS,同样的是早先与APACHE合作</ p>

Nginx配置</ p>

  location / oam_restapi / restapi / {
满足任何;
add_header Access-Control-Allow-Origin *;
root / html / oam_restapi / RESTAPI /;

index index.php;

try_files $ uri /oam_restapi/restapi/index.php;

}
</ code> </ pre>
</ div>

展开原文

原文

I am using slim php framework using nginx , i have enabled CORS also . Now when i make a request to my Rest API from some other domain, the option method is invoked first , then post method is invoked with actual post data. Response for POST is 200 but there is no data coming from API .

But when i try the same with same domain , everything works perfect.

WORKING

$.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);


    }

});

when this ajax call is made like 172.19.4.130/makeajax.html

Not working

When this same call is made like localhost/makeajax.html

I understand CORS , and the same was working with APACHE earlier

Nginx Configuration

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; 
}

1个回答



您需要在后端的响应中使用 Access-Control-Allow-Origin </ code>标头。< / p>

查看 https://github.com/palanik/CorsSlim</ a> </ p>
</ div>

展开原文

原文

You need to have the Access-Control-Allow-Origin header in the response from the backend.

Have a look at https://github.com/palanik/CorsSlim

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问