Ajax请求将冻结我的Javascript代码

时间:2012-12-21 12:48:18

标签: javascript jquery html ruby-on-rails-3

我有一个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'有一个loop..do,永远不会结束!我明白这可能是问题所在。所以我将ajax请求更改为POST 1:

$.ajax({
    url: '/test/start_images_stream',
    type: "POST",
    data : { image_id: tst },
    dataType: "json",
    complete: function () {
        stream_images();
    }
});

并将我的服务器代码改编为它。现在ajax请求似乎再次冻结我的主线程。 我只想对服务器说,启动'功能'产生无限数量的图像,就是这样。然后Javascript读取文件并在HTML页面上列出它们。

它似乎无法正常工作

2 个答案:

答案 0 :(得分:3)

我认为你的完整功能:

complete: function () {
        stream_images();
    }

应该在success属性中工作,就像那样:

success: function () {
        stream_images();
    }

答案 1 :(得分:0)

status = data_response;

回调将结果作为参数传递(如果指定了json,则为字符串或对象),它没有responseText属性。

jQuery抽象出这些东西,所以你不必担心知道responseText属性,只需得到结果。