weixin_43378514
Smile Taotao cium
2019-07-01 17:58
采纳率: 22.2%
浏览 2.0k
已采纳

SockJS连接跨域问题 报错

前端代码
//var url="http://192.168.8.222:88/mi/connectServer"
var socket = new SockJS(url);
stompClient = Stomp.over(socket);
stompClient.connect({
Referer:""
}, function(frame) {
// setConnected(true);
console.log(randomCode);
stompClient.subscribe('/getMessage/' + randomCode + '/message',function(greeting){
var msg = JSON.parse(greeting.body);
console.log(JSON.parse(greeting.body));
if(msg.codeNum==0){
mui.openWindow("pay-success.html?msg="+greeting.body)
}else {
mui.openWindow("pay-fail.html")
}
});
});
后台代码

public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
//在网页上可以通过"/applicationName/hello"来和服务器的WebSocket连接
stompEndpointRegistry.addEndpoint("/mi/connectServer").setAllowedOrigins("*").withSockJS();
}

主要是前后端分离 出现这个问题
Access to XMLHttpRequest at 'http://mms-server.swxc.com/mi/connectServer/info?t=1561973806317' from origin 'http://192.168.8.222:5000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • weixin_43378514
    Smile Taotao cium 2019-07-01 19:39
    已采纳

    自己有个CORSInterceptor 拦截器 response.addHeader("Access-Control-Allow-Origin", “*”);
    修改为
    response.addHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
    response.addHeader("Access-Control-Allow-Credentials", "true");
    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    就好了

    点赞 评论

相关推荐