码迷,mamicode.com
首页 > 其他好文 > 详细

跨域问题

时间:2016-11-26 22:46:35      阅读:518      评论:0      收藏:0      [点我收藏+]

标签:127.0.0.1   lan   使用   etl   ejs   cannot   methods   访问   list   

当利用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");是有兼容性问题的,会发出两次请求

 

跨域问题

标签:127.0.0.1   lan   使用   etl   ejs   cannot   methods   访问   list   

原文地址:http://www.cnblogs.com/jolene/p/6105160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!