【发布时间】:2014-09-03 07:41:34
【问题描述】:
我正在尝试从本地 Wamp 服务器调用 CTA API (http://www.transitchicago.com/developers/bustracker.aspx)。但是,当通过骨干集合进行提取时,我得到:
XMLHttpRequest cannot load http://www.ctabustracker.com/bustime/api/v1/getroutes?key=xx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
收藏:
define([
'models/route',
'core'
], function (Route) {
return Backbone.Collection.extend({
initialize: function () {},
model: Route,
//url: function () {
// return 'http://www.ctabustracker.com/bustime/api/v1/getroutes?key=xx';
//},
url: function () {
return '/apiproxy.php?method=getroutes';
},
});
});
我知道这是一个常见问题,但尚未找到简明的答案。
我该如何解决这个问题? 更新 添加了 apiproxy,但收到此响应:
Remote Address:127.0.0.1:80
Request URL:http://localhost/apiproxy.php?method=getroutes
Request Method:GET
Status Code:200 OK
Request Headersview parsed
GET /apiproxy.php?method=getroutes HTTP/1.1
控制台:
responseText: "$url = "http://www.ctabustracker.com/bustime/api/v1/{$_GET['method']}? key=xx";
↵echo file_get_contents($url);
SyntaxError {stack: (...), message: "Unexpected token $"}
message: "Unexpected token $"
stack: (...)
【问题讨论】:
-
您尝试访问的服务器不允许从外部ip远程请求值传输
-
您应该响应标头 Access-Control-Allow-Origin: * from the server to allow cross domain
-
我猜这是因为您是从本地主机执行此操作,但问题是您请求的 URL 不允许您的请求
标签: javascript ajax backbone.js