前端访问接口报错

直接访问没问题
图片描述


fetch('http://192.168.43.218:8080/tvseries',{

    method: 'GET',
})
.then(function(response) {
    console.log(response);
})
.then(function(myJson) {
    console.log(myJson);
});

本地项目用 nginx启动的


报错是
图片描述

阅读 4k
3 个回答

1: 可以用 fiddler 去代理你的请求.

2: 本地启用了 nginx 那么直接用nginx 作以下代理转发
这里需要修改下配置文件 nginx.conf 如下:

    http {
        server {
                
            location /api/ {
                proxy_pass 'http://192.168.43.218:8080/';
            }
        }
    }

api 是请求接口的前缀, 只把这些请求作代理; 请求更改如下


fetch('http://localhost:8080/api/tvseries',{

    method: 'GET',
})
.then(function(response) {
    console.log(response);
})
.then(function(myJson) {
    console.log(myJson);
});

8080 表示是 nginx 的端口, 根据相应的作修改

跨域了,让后端解决跨域。

新手上路,请多包涵

nginx配置server块中添加以下配置,重启nginx即可:

add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS,DELETE';
if ($request_method = OPTIONS) {
    return 200;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题