Heim  >  Fragen und Antworten  >  Hauptteil

javascript - nginx反向代理解决跨域的疑问

localhost:80下的页面通过ajax,去调取localhost:8082/api/v1.0/testapi的数据,如下nginx配置问题在哪儿?谢谢

server {

    listen       80;
    server_name  localhost;

    location /api/v1.0 {
        proxy_pass http://localhost:8082/api/v1.0;
    }
    location / {
        root   html;
        index  index.html index.htm;
    }

}

ajax调用:

$.ajax({

url:'/api/v1.0/testapi',
success: function(data){
    console.log(data)
}

});

PHP中文网PHP中文网2718 Tage vor284

Antworte allen(2)Ich werde antworten

  • 阿神

    阿神2017-04-11 12:28:51

    server块中 server_name下面追加头信息

    server {
        .....
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Credentials true;
        ....
    }
    
    

    另外* 可以替换成具体的请求URL地址

    Antwort
    0
  • PHPz

    PHPz2017-04-11 12:28:51

    似乎木有看到错误

    Antwort
    0
  • StornierenAntwort