为什么会出现这种跨域Ajax调用实际工作? [英] Why does this cross-domain ajax call actually work?

查看:135
本文介绍了为什么会出现这种跨域Ajax调用实际工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无意中写了一个跨域AJAX调用 NextBus (使用jQuery):

I inadvertently wrote a cross-domain AJAX call to NextBus (with jQuery):

$.ajax({
      url: 'http://webservices.nextbus.com/service/publicXMLFeed?command=predictions&a=sf-muni&r=1&s=6294',
      dataType: 'xml',
      success: function(data) {
           do_stuff();
      }
});

事情是,它适用于所有的浏览器,尽管来自不同的领域来了。鉴于单一起源策略,为什么会发生这种实际工作?

该网页是在这里: http://sftransitfirst.org/F/ ,从下拉选择止损触发阿贾克斯。

The page is here: http://sftransitfirst.org/F/, selecting a stop from the pull-down triggers the ajax.

正如预期的那样,制定类似的调用谷歌地图API的Web服务失败熟悉的原点......不被访问控制 - 允许 - 原产地允许的(而且不支持JSONP)。

As expected, making a similar call to the Google Maps API Web Services fails with the familiar Origin ... is not allowed by Access-Control-Allow-Origin (and it doesn't support jsonp).

推荐答案

他们必须明确允许跨域访问,带着几分这样的方式:

They must have explicitly allowed cross-domain access, with something of this manner:

<?php header('Access-Control-Allow-Origin: *'); ?>

或用htaccess的:

Or with htaccess:

<ifModule mod_headers.c>
    Header set Access-Control-Allow-Origin: *
</ifModule>

这篇关于为什么会出现这种跨域Ajax调用实际工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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