XMLHtt prequest无法加载不是由访问控制允许来源允许 [英] XMLHttpRequest cannot load is not allowed by Access-Control-Allow-Origin
问题描述
我想可以访问education.com API数据。不过,我不断收到一个错误的错误状态:
I'm trying to get access to education.com API data. However, I keep receiving an error the error states:
XMLHtt prequest无法加载<一个href=\"http://api.education.com/service/service.php?f=schoolSearch&key=mykey&sn=sf&v=4&city=Atlanta&state=ga&Resf=json\">http://api.education.com/service/service.php?f=schoolSearch&key=mykey&sn=sf&v=4&city=Atlanta&state=ga&Resf=json.原产地不被访问控制允许来源允许的。
XMLHttpRequest cannot load http://api.education.com/service/service.php?f=schoolSearch&key=mykey&sn=sf&v=4&city=Atlanta&state=ga&Resf=json. Origin is not allowed by Access-Control-Allow-Origin.
我的code是以下内容:
My code is the following:
$(function(){
$.getJSON('http://api.education.com/service/service.php?f=schoolSearch&
key=mykey&sn=sf&v=4&city=Atlanta&state=ga&Resf=json',
function(data) {
console.log(data);
});
});
有人能帮助我吗?
Can someone help me please?
推荐答案
上一篇文章跨域AJAX 问题而回这里:
An article on Cross Domain AJAX issue a while back here:
<一个href=\"http://www.cy$p$pssnorth.com/blog/programming/cross-domain-ajax-request-with-json-response-for-iefirefoxchrome-safari-jquery/\">CROSS使用JSON响应中心进行IE,火狐,Chrome,Safari浏览器域AJAX请求 - JQUERY
如果您有响应的服务器的控制权来处理最简单的方法是添加一个响应头为:
The easiest way to handle this if you have control of the responding server is to add a response header for:
Access-Control-Allow-Origin: *
这将允许跨域AJAX。在PHP中,你会想要修改,像这样的回应:
This will allow cross domain AJAX. In PHP you'll want to modify the response like so:
<?php header('Access-Control-Allow-Origin: *'); ?>
你可以把页眉设置访问控制允许来源*设置在Apache的conf或htaccess文件,它只是像一个魅力
you can just put Header set Access-Control-Allow-Origin * setting on apache conf or htaccess file it just work like a charm
编辑:
从的意见,这是一个重要的注意事项:通配符是要
允许任何域将请求发送到您的主机。我建议更换
与您将运行脚本的特定域的星号
在
From the comments, this is an important note: the wildcard is going to allow any domain to send requests to your host. I recommend replacing the asterisk with a specific domain that you will be running scripts on
这篇关于XMLHtt prequest无法加载不是由访问控制允许来源允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!