Ajax跨域get出现的Not allowed by Access-Control-Allow-Origin
来源:互联网 发布:mac命令行终端软件 编辑:程序博客网 时间:2024/06/05 20:12
ajax在跨域get的时候会出现如标题所示的error get本地文件就不会 见如下代码
//get 本地$.ajax({ url:"http://localhost/tickets/json/api_airport.json", type:'GET', dataType:"json", success:function(data){console.log(data.results.result[1].category);} });
//跨域get$.ajax({ url:"http://api.master18.tiket.com/search/autocomplete/hotel?q=mah&token=90d2fad44172390b11527557e6250e50&secretkey=83e2f0484edbd2ad6fc9888c1e30ea44&output=json", type:'GET', crossDomain:true, beforeSend: function(x) { if(x && x.overrideMimeType) { x.overrideMimeType("application/j-son;charset=UTF-8"); } }, success:function(data){console.log("Success");} });
<?php// File Name: proxy.phpif (!isset($_GET['url'])) die();$url = urldecode($_GET['url']);$url = 'http://' . str_replace('http://', '', $url); // Avoid accessing the file systemecho file_get_contents($url);
ajax改为:
$.ajax({ url:"proxy.php", data: "url=http%3A%2F%2Fapi.master18.tiket.com%2Fsearch%2Fautocomplete%2Fhotel%3Fq%3Dmah%26token%3D90d2fad44172390b11527557e6250e50% 26secretkey%3D83e2f0484edbd2ad6fc9888c1e30ea44%26output%3Djson", type:'GET', dataType:"json", success:function(data){console.log(data.results.result[1].category);}});
And the reason:
You're getting this error because of XMLHttpRequest same origin policy, which basically boils down to a restriction of ajax requests to URLs with a different port, domain or protocol. This restriction is in place to prevent cross-site scripting (XSS) attacks.
0 0
- Ajax跨域get出现的Not allowed by Access-Control-Allow-Origin
- JQuery 的 ajax 出现Origin null is not allowed by Access-Control-Allow-Origin 解决方法
- Origin null is not allowed by Access-Control-Allow-Origin.
- Origin null is not allowed by Access-Control-Allow-Origin
- 解决Ajax跨域问题:Origin xx is not allowed by Access-Control-Allow-Origin.
- 解决Ajax跨域问题:Origin xx is not allowed by Access-Control-Allow-Origin
- Chrome 出现Origin null is not allowed by Access-Control-Allow-Origin.
- 使用chrome调试ajax加载本地文件的时候出现Failed to load resource: Origin null is not allowed by Access-Control-Allow
- 让chrome支持本地Ajax请求,Ajax请求status cancel Origin null is not allowed by Access-Control-Allow-Origin
- chrome发起跨域ajax请求遇到 Origin null is not allowed by Access-Control-Allow-Origin 一解
- 在当地使用Chrome中调试ajax时提示Origin null is not allowed by Access-Control-Allow-Origin
- XMLHttpRequest cannot load – Origin is not allowed by Access-Control-Allow-Origin.
- XMLHttpRequest Origin null is not allowed by Access-Control-Allow-Origin
- XMLHttpRequest在请求失败(Origin null is not allowed by Access-Control-Allow-Origin.)
- Origin XX is not allowed by Access-Control-Allow-Origin.解决办法
- Origin null is not allowed by Access-Control-Allow-Origin解决方案
- XMLHttpRequest在请求失败(Origin null is not allowed by Access-Control-Allow-Origin.)
- XMLHttpRequest cannot load – Origin is not allowed by Access-Control-Allow-Origin.
- 我与c++在一起的生活
- 性能考量的代码编写约束
- 构建两个路径的关系
- EXPECT介绍
- 无法更新 Android SDK Tools--"be accessing the directory 'E:\Android\android-sdk-windows\tools"
- Ajax跨域get出现的Not allowed by Access-Control-Allow-Origin
- Centos6如何更改yum源为163
- UML(十)构件图
- SharePoint中的一些常量
- Apk如何调用非系统jar包
- 虚拟内存技术
- Android 如何将非系统jar包打入系统包
- spring定时器及cron表达式
- 人生与黑苹果2014