Smile Taotao cium 2019-07-01 17:58 采纳率: 50%
浏览 3285
已采纳

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条回答 默认 最新

  • 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");
    就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来