0

所以我有一个场景,我的 jQuery ajax 请求正在访问服务器,但页面不会更新。我被难住了...

这是ajax请求:

$.ajax({
    type: 'GET',
    url: '/jsrender',
    data: "id=" + $.fragment().nav.replace("_link", "")
});

观看 rails 日志,我得到以下信息:

Processing ProductsController#jsrender (for 127.0.0.1 at 2010-03-17 23:07:35) [GET]
Parameters: {"action"=>"jsrender", "id"=>"products", "controller"=>"products"}
  ...
Rendering products/jsrender.rjs
Completed in 651ms (View: 608, DB: 17) | 200 OK [http://localhost/jsrender?id=products]

因此,在我看来,ajax 请求正在到达服务器。jsrender 方法中的代码正在执行,但 jsrender.rjs 中的代码没有触发。这是方法,jsrender:

def jsrender
    @currentview = "shared/#{params[:id]}"   
    respond_to do |format|
        format.js {render :template => 'products/jsrender.rjs'}
    end
end

为了论证起见,jsrender.rjs 中的代码是:

page<<"alert('this works!');"

为什么是这样?我在参数中看到没有authenticity_token,但我也尝试过传递authenticity_token,结果相同。

提前致谢。

4

1 回答 1

2

从我在这里可以看到,您的请求正在服务器上处理,并且正在返回响应,但是如果您希望它被处理/评估,您必须在 $.ajax 调用中添加一个回调函数,您可以在其中告诉它如何处理响应。

$.ajax({
  type: 'GET',
  url: '/jsrender',
  data: "id=" + $.fragment().nav.replace("_link", ""),
  success: function(response) {
    eval(response);
  }
});

您可以在此处找到 jQuery 的 $.ajax 调用的所有可用参数。

我还建议使用Firebug(或等效的,如果您不使用 Firefox)来调试您的 ajax 请求。
它有一个控制台选项卡,您可以在其中查看发出的每个 XHR 请求、请求/响应标头是什么、发布的数据和响应状态代码。

此外,不需要发送 ,authenticity_token因为您正在执行 aGET并且仅在非 GET 请求时才需要令牌。

于 2010-03-19T14:35:30.057 回答