【发布时间】:2012-12-21 12:48:17
【问题描述】:
我有一个 Ajax 请求:
$.ajax({
url: '/test/start_images_stream',
type: "GET",
data : { image_id: tst },
dataType: "json",
complete: function (data_response) {
status = data_response.responseText;
console.log("The response was: "+status);
stream_images();
}
});
这似乎冻结了我的 javascript。它停留在 ajax 请求上,并没有移动到 stream_images()。
在我的服务器上我得到了这个:
def start_images_stream
#ajax request parameters
tst=params[:image_id]
start_method
respond_to do |format|
format.json { render :json => "OK"}
end
end
'start_method' 有一个循环..do,它永远不会结束!我明白这可能是问题所在。所以我将我的 ajax 请求更改为 POST 请求:
$.ajax({
url: '/test/start_images_stream',
type: "POST",
data : { image_id: tst },
dataType: "json",
complete: function () {
stream_images();
}
});
并调整了我的服务器代码。现在,ajax 请求似乎再次冻结了我的主线程。 我只想对服务器说,启动“功能 a”,生成无限数量的图像,仅此而已。然后 Javascript 读取文件并在 HTML 页面上列出它们。
好像不行
【问题讨论】:
-
因此,如果您在 $.ajax 块之外添加代码,那么在处理您的 Ajax 请求时不会调用该代码?我只是想确保您的线程真的被冻结了...另外,您是否尝试过更改服务器端的程序,使其只返回一个简单的字符串,以消除该端的任何算法问题?
-
好的,我要试试你的想法
-
是的,如果我把它放在外面就可以了。谢谢。最后一个问题,ajax请求会继续执行吗?
-
Ajax 请求继续执行,但过一段时间就会超时(超时时间取决于你的服务器配置)。这就是为什么必须将代码放入回调中的原因,因为请求是异步运行的(在后台)。
标签: javascript jquery html ruby-on-rails-3