问题:
我正在尝试使用JSON跨域访问,但我找到的都是JSON解析器,而我并不需要……
我读到可以使用JSON进行跨域请求,但到目前为止,我所看到的都是使用XMLHttpRequest的实现……
我一直在用http://www.json.org/,但我找到的要么是解析器,要么是无用的。
到目前为止,我发现的最好的google是
http://devpro.it/JSON/files/JSONRequest-js.html
但这相当混乱,不能跨域工作,也不能在域内工作--或者更确切地说,根本不能工作……
var the_object = {};
var http_request = new XMLHttpRequest();
http_request.open( "GET", url, true );
http_request.onreadystatechange = function () {
if ( http_request.readyState == 4 && http_request.status == 200 ) {
the_object = JSON.parse( http_request.responseText );
}
};
http_request.send(null);
发布于 2010-01-13 15:58:23
你可以做的跨域是注入一个脚本,包括:
var s = document.createElement('script');
s.src = 'http://someotherdomain/getMeMyJs.aspx?parameter=value';
s.onload = someOptionalCallback;
s.type = 'text/javascript';
if(document.getElementsByTagName('head').length > 0)
document.getElementsByTagName('head')[0].appendChild(s);
现在,该请求返回的代码将立即执行。如果你想让它与你的代码交互,你可以确保它与所有封装在函数调用中的数据一起返回:
jsonCallback({ object: json, whatever: value });
您可以使用它来构建API,在API中将回调函数的名称作为请求querystring参数进行传递。Here's an example of such an API
https://stackoverflow.com/questions/2055186
复制相似问题