首页 > 代码库 > 跨域问题

跨域问题

当利用ajax请求本地,如 http://192.168.112.114:8081/api/getlist?PageIndex=1出现跨域问题提示:

 XMLHttpRequest cannot load http://192.168.112.114:8081/api/getlist?PageIndex=1. Response to preflight request doesn‘t pass access control check: No ‘Access-Control-Allow-Origin‘ header is present on the requested resource. Origin ‘http://127.0.0.1:8020‘ is therefore not allowed access. 

跨域组织是浏览器的行为,查了一下解决方法如下:


1、用jsonp方式去访问
2、只需要在NodeJS服务器中添加几个响应报文即可完成
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");

如果使用mui中的getJson()方法对于res.header("Access-Control-Allow-Headers", "X-Requested-With");是有兼容性问题的,会发出两次请求

 

跨域问题