【发布时间】:2014-05-18 20:10:00
【问题描述】:
这是一篇关于跨域访问资源的answer:
The XHR is constrained by cross-domain rules; to use JSONP you need to add a script element:
function func_callbk()
{
console.log(arguments);
}
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = 'http://abhishekprakash.com/script/example.json?callback=func_callbk';
var h = document.getElementsByTagName('script')[0];
h.parentNode.insertBefore(s, h);
As pointed out by Ian in the comments, the proper response of your server should be something like this:
func_callbk('hello world')
问题:
- 如果您无法跨域访问资源,则该资源将无法访问,或者至少浏览器不允许该域访问其他域:
s.src = 'http://abhishekprakash.com/script/example.json?callback=func_callbk';对吗? - 当访问上述资源时,握手中的兼容服务器应返回
func_callbk('hello world')的字符串?
【问题讨论】:
-
你的问题不是很清楚。 JSONP 利用了这样一个事实,即通常浏览器允许脚本源位于与主机页面不同的域中。是的,符合 JSONP 的服务器返回其 JSON 响应,该响应包含在对您提供的名称的函数的调用中。
标签: javascript http cross-domain jsonp