Firefox/Chrome/IE Ajax 怎样设置允许跨域请求
来源:互联网 发布:mysql tmp table size 编辑:程序博客网 时间:2024/05/17 21:42
一般js出现跨域请求时时,浏览器错误为
chrome错误:
XMLHttpRequest cannot load xxx.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
Firefox错误:
已阻止跨域源请求:同源策略禁止取位于 http://...xxx 的远程资源.(原因: CORS 头缺少 'Access-Control-Allow-Origin')
IE错误:
SEC7118: http://127.0.0.1/data/login.php 的 XMLHttpRequest 需要跨域资源共享(CORS)。SEC7120: 在 Access-Control-Allow-Origin 标头中未找到源 http://localhost:8080。SCRIPT7002: XMLHttpRequest: 网络错误 0x80070005, 拒绝访问。
其实这个错误提示已经很明显了,只要在请求的服务器端的页面加个 CORS 头就好了,但这个设置还是有点技巧的,请看如下php页面代码
<?phpheader("Access-Control-Allow-Origin: http://localhost:8080");sleep(3);header("Content-type:text/html;charset=utf-8");$username = $_POST['name'];$pass = $_POST['pass'];$arr["success"] =1;echo json_encode($arr);?>
关键:
header("Access-Control-Allow-Origin: http://localhost:8080");
加这个header就可以让你的请求顺利跨域了,注意参数值必须是准确的域名,不能直接
header("Access-Control-Allow-Origin: *"); //这样是不行的,因为标准规范说不允许广泛匹配
我在localhost:8000的webpack-dev-server,请求本地的php server localhost:80/data/login.php
var url = "http://127.0.0.1/data/login.php"; var cont={}; cont.name = name; cont.pass = pass; var result=0;//ajax请求是否正确 $.ajax({ url:url, type:'post', dataType:'json', data:cont, success:function(data){ result = data.success; console.log("error result=",result); window.location.href="/#/info"; }, error:function(data){ result = data.success; console.log("error result=",result); } }); console.log("ajax quest done!")
测试这样设置Firefox/Chrome/IE都可以正常访问!!!
至于网上说的Firefox设置about:config,还有在ajax open 方法前运行一行代码那都是扯淡,完全不能用(也可能是高级版本浏览器已经禁用这样的方法吧, 不知道是否如此)!!!
大家有需要可能试一下这样的写法!!!!
阅读全文
0 0
- Firefox/Chrome/IE Ajax 怎样设置允许跨域请求
- 设置apache允许跨域 ajax请求
- IE,firefox , chrome设置圆角 css
- IE,firefox , chrome设置圆角 css
- 兼容ie,firefox,chrome的跨域加载js脚本
- chrome调试ajax 跨域请求
- ajax中获取XMLHttpRequest对象的方法(IE/Firefox/chrome)
- IE、Firefox、Chrome比较
- 设置或解除ie,firefox,chrome为默认浏览器
- CSS透明度设置支持IE,Chrome,Firefox浏览器
- CSS透明度设置支持IE,Chrome,Firefox浏览器
- ajax跨域请求,支持js的ajax跨域请求,只需要设置后台代码的返回头允许跨域
- PHP允许AJAX跨域请求的两种方法
- Mac Chrome允许跨域
- 只允许ajax请求
- PHP设置HTML5允许AJAX跨域GET请求
- firefox ie 下ajax
- pasteHTML兼容ie,firefox,chrome
- HDU-3595 GG and MM
- HDU 1704 Rank floyd求传递闭包 || bfs
- idea 注册码和汉化包
- js中阻止默认事件
- 查看点a文件
- Firefox/Chrome/IE Ajax 怎样设置允许跨域请求
- mongodb常用操作
- 昆石VOS3000_2.1.2.0一键安装教程
- Tomcat 7最大并发连接数的正确修改方法
- Android
- Linux网络编程——TCP服务器
- SQL Server 字符串 系统常用函数 贴心
- I/O流常用的简化操作
- JAVA入门