NutzCN Logo
问答 前后端分离后出现跨越问题
发布于 96天前 作者 fabienfay 282 次浏览 复制 上一个帖子 下一个帖子
标签:

怎么在后端设置
Access-Control-Allow-Origin: *

8 回复

你要解决的不是跨域问题, 要解决的是前后端用同一个端口的问题

那有什么解决方法呢

Failed to load http://192.168.70.202:8081/user/getInitData?_=1562811870740: Redirect from 'http://192.168.70.202:8081/user/getInitData?_=1562811870740' to 'http://192.168.70.202:8081/login;JSESSIONID=da340b10-0042-47e9-a303-f15524fb9211' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.

@Filters({@By(type = CrossOriginFilte.class)})
如果有自定义请求头,自己实现替换上面的类

public class MyCrossOriginFilter extends CrossOriginFilter {


    // 增加deviceId token 请求头
    public MyCrossOriginFilter() {
        super();
        headers = "Origin, Content-Type, Accept, X-Requested-With, deviceId, token";
    }
}

前端页面的server,把后端请求代理到后端去

如果是开发时候调试的话可以装一个Chrome跨域插件

@At
@Ok("raw")
@Filters({@By(type=CrossOriginFilter.class, args={"*", "GET, POST, PUT, DELETE, OPTIONS, PATCH", "Origin, Content-Type, Accept, X-Requested-With", "true"})})
public String getInitData(@Attr("me") int me){
String data = userDao.getInitData(me);
// System.out.println(data);
return data;
}

报错
Access to XMLHttpRequest at 'http://192.168.70.202:8081/user/getInitData?_=1563174382888' from origin 'http://127.0.0.1:3001' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

```

添加回复
请先登陆
回到顶部