我正在尝试访问education.com API数据。然而,我一直收到一个错误,错误状态:
我的代码如下:XMLHttpRequest无法加载http://api.education.com/service/service.php?f=schoolSearch&关键= mykey& sn = sf& v = 4,城市= Atlanta&状态= ga& Resf = json。Access-Control-Allow-Origin不允许使用Origin
$(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);
});
});
有人能帮我一下吗? 你不能用浏览器这样做,除非教育网服务器被配置为允许CORS (Cross-Origin Resource Sharing
)请求(这是Access-Control-Allow-Origin
位)。
您的服务器可以代表您发出请求,但是,您可以从那里检索数据。
在ZF1中可以这样做:
public function indexAction() {
$turnkey = array(
"uris" => array("176.x.x:3478", "3478"),
"username" => "my_username",
"password" => "my_password"
);
$content = Zend_Json::encode($turnkey);
$this->getResponse()
->setHeader('Access-Control-Allow-Origin', "*")
->setHeader('Content-Type', 'application/json')
->setBody($content)
->sendResponse();
exit;
}
不是解决问题的办法,而是一个避免问题的hack——在没有网络安全的情况下启动浏览器:
in MacOS this would work as follows: cd to ../../Applications/Google Chrome.app/Contents/MacOS
then start the brwoser with the appropiate flag: Google Chrome --disable-web-security'
注意:永远不要使用浏览器进行正常的冲浪,这只是为了让你在紧急情况下!欢呼声