jQuery跨域ajax:执行时回调 背景
我正在通过jQuery的jQuery跨域ajax:执行时回调 背景,jquery,ajax,callback,cross-domain,same-origin-policy,Jquery,Ajax,Callback,Cross Domain,Same Origin Policy,我正在通过jQuery的.ajax(…)调用从另一台服务器(跨域)加载和执行脚本 <html><head> ... <script language="javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script> <script language="javascript" src="http://example.com/script-from-other-s
.ajax(…)
调用从另一台服务器(跨域)加载和执行脚本
<html><head>
...
<script language="javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script language="javascript" src="http://example.com/script-from-other-server.js"></script>
</head><body></body></html>
在执行另一台服务器的代码后,需要执行一些代码,因为在其他情况下,有些对象是“未定义的”
可能很重要:远程代码确实包含另一个getScript(…)
调用。我还得等待这段代码被执行。我不能简单地从代码中加载第二个脚本,因为它的源代码是动态的(即,取决于远程脚本的某些结果)
未工作:成功
回调
显然,在远程代码加载之后调用success
回调,但在执行远程代码之前调用
# coffee script
executeLater = ->
# this bit of code needs to run after the remote code has been executed.
console.log("yehaa!")
$.getScript("http://example.com/script-from-other-server.js")
.success(executeLater) # this gets executed when the remote script is loaded,
# but before the remote script is executed.
不工作:async:false
显然,对于跨域请求,async
属性被忽略,如jQuery文档中所述:
此外,我希望避免使用async:false
设置,因为据说它会阻止浏览器
# coffee script
executeLater = ->
# this bit of code needs to run after the remote code has been executed.
console.log("yehaa!")
$.ajax(
dataType: 'script',
url: 'http://example.com/script-from-other-server.js',
async: false # does not work because the request is cross domain
)
.success(executeLater)
不起作用:$。当(…)。然后(…)
显然,使用jQuery时,then
代码在执行when块之前执行
# coffee script
executeLater = ->
# this bit of code needs to run after the remote code has been executed.
console.log("yehaa!")
$.when( $.ajax(
dataType: 'script',
url: 'http://example.com/script-from-other-server.js',
) ).then(executeLater)
编辑:确实有效,但无法使用:ajax
两个脚本
我不能在生产环境中这样做,正如我在“后台”一节中所说的,但是如果我将所有情况减少到一个,并在我自己的脚本中加载第二个脚本(通常由远程脚本执行),那么所有这些都可以正常工作
# coffee script
executeLater = ->
# this bit of code needs to run after the remote code has been executed.
console.log("yehaa!")
$.getScript("http://example.com/script-from-other-server.js")
.success( ->
$.ajax(
dataType: 'script',
cache: true,
# I do not know this url in production:
url: 'http://example.com/another-script-from-the-remote-server.js'
)
.success(executeLater)
)
避免的事情
我不喜欢使用像两个setTimout
调用这样的构造,直到某个对象被定义为执行executeLater()
方法
我需要的是:执行回调
最好使用一种执行的
回调,而不是ajax
方法的success
回调。但是,到目前为止,我还没有找到这个回调
# coffee script
executeLater = ->
# this bit of code needs to run after the remote code has been executed.
console.log("yehaa!")
$.ajax(
dataType: 'script',
url: 'http://example.com/script-from-other-server.js',
executed: executeLater # <---- I NEED A CALLBACK LIKE THIS
)
这项工作:
根据,同源策略允许html
标记加载的远程脚本通过ajax
加载其他远程脚本
<html><head>
<script language="javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script language="javascript" src="http://example.com/script-from-other-server.js"></script>
</head><body></body></html>
问题仍然存在:是否有一个好方法可以通过ajax调用来实现这一点,或者,我是否必须通过在html文档中插入一个
标记来“证明”我“拥有这段代码”?这有点令人讨厌,但是您是否尝试过使用iframe
?想法很简单:
准备一个带有操作
和方法
的表单,该表单适合您试图提出的请求
将表单的目标设置为指向页面上的iframe
,此iframe
可以隐藏
收听iframe
的onload
,当iframe
加载后,您可以执行代码
下面是一个小示例(使用jQuery):
变量形式=$(“#变换”),
iframe=$(“#xiframe”);
iframe.load(函数(){
//把你的东西放在这里
});
表单提交();
Background
<> > Addio建议考虑<强> JavaScript同源策略<>强(见问题评论)确实解决了我的问题。
如果问题假设在请求的脚本完全执行之前调用了success
回调,那么真正的问题是,请求的脚本确实请求了另一个脚本,如问题中所述:
可能很重要:远程代码确实包含另一个getScript(…)
调用。我还得等待这段代码被执行。我不能简单地从代码中加载第二个脚本,因为它的源代码是动态的(即,取决于远程脚本的某些结果)
当动态加载请求的脚本时,JavaScript的同源策略会阻止第二个getScript
调用
解决方案1:在html代码中包含脚本
如果有权访问html文件,可以添加一个脚本标记,远程脚本名为src
。因此,我们“证明”我们确实想要加载这个远程脚本,javascript将执行远程getScript
调用
<html><head>
...
<script language="javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script language="javascript" src="http://example.com/script-from-other-server.js"></script>
</head><body></body></html>
解决方案2:规避某些原产地政策
不建议这样做,但这是可能的。
存在一个常见的堆栈溢出问题,即如何绕过同源策略:
解决方案3:真正等待脚本执行
如果除了同源策略外,远程脚本的执行时间确实太长,以至于本地脚本必须等待,则可以使用Ahmed Nuaman的iframe解决方案:
这是一个格式非常好的问题,但最大的问题仍然是,你确定javascripts同源策略不是问题吗?@adeneo:我不确定,我怎么才能找到答案?我刚刚添加了“确实有效,但无法使用:ajax和脚本”。这回答了你的问题吗?@adeneo:看来你是对的:SOP可能是问题所在。请查看“编辑:同一原产地政策”部分。你怎么想?就像你说的。。。您可能需要将脚本添加到dom元素以克服SOP问题。。。但第二个问题仍然存在。。。当动态脚本加载完成时,将触发一个处理程序。您是否能够对第一个动态脚本元素进行一些更改?感谢您提出此修复方案。据我所知,它确实允许等待脚本执行,而不是仅仅成功加载。但是,正如adeneo在上面的评论部分所建议的那样,真正的问题是我没有考虑js单一原产地政策。纠正这一点(通过将脚本标记直接包含到html结构中)确实解决了我的问题。。。现在,我不知道如何在stackoverflow中正确进行。我是否接受你的答案(因为它可能回答了我的问题)
<html><head>
...
<script language="javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script language="javascript" src="http://example.com/script-from-other-server.js"></script>
</head><body></body></html>
# coffee script
executeLater = ->
# this bit of code needs to run after the remote code has been executed.
console.log("yehaa!")
$(document).ready(executeLater)