Access-Control-Allow-Origin 不允许 XMLHttpRequest 无法加载 [英] XMLHttpRequest cannot load is not allowed by Access-Control-Allow-Origin

查看:45
本文介绍了Access-Control-Allow-Origin 不允许 XMLHttpRequest 无法加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问education.com API 数据.但是,我不断收到错误状态的错误:

I'm trying to get access to education.com API data. However, I keep receiving an error the error states:

XMLHttpRequest 无法加载http://api.education.com/service/service.php?f=schoolSearch&key=mykey&sn=sf&v=4&city=Atlanta&state=ga&Resf=json.Access-Control-Allow-Origin 不允许 Origin.

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.

我的代码如下:

$(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);
    });
});

有人可以帮我吗?

推荐答案

一篇关于 Cross Domain AJAX 问题的文章回到这里:

An article on Cross Domain AJAX issue a while back here:

跨域 AJAX 请求和 JSON 响应对于 IE、Firefox、CHROME、SAFARI – 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: *'); ?>

您可以将标题集 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

重要说明:
通配符将允许任何域向您的主机发送请求.我建议将星号替换为您将在其上运行脚本的特定域.

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.

这篇关于Access-Control-Allow-Origin 不允许 XMLHttpRequest 无法加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆