Ajax 请求到达服务器但页面未更新 - Rails、jQuery

发布于 2024-08-25 18:03:38 字数 1031 浏览 5 评论 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,结果相同。

提前致谢。

So I have a scenario where my jQuery ajax request is hitting the server, but the page won't update. I'm stumped...

Here's the ajax request:

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

Watching the rails logs, I get the following:

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]

So, it seems apparent to me that the ajax request is getting to the server. The code in the jsrender method is being executed, but the code in the jsrender.rjs doesn't fire. Here's the method, jsrender:

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

For the sake of argument, the code in jsrender.rjs is:

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

Why is this? I see in the params that there is no authenticity_token, but I have tried passing an authenticity_token as well with the same result.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

并安 2024-09-01 18:03:38

从我在这里可以看到,您的请求正在服务器上处理,并且正在返回响应,但是如果您希望处理/评估它,您必须向 $.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,因为您正在执行 GET,并且仅在非 GET 请求时才需要令牌。

From what I can see here, your request IS being processed on the server, and the response is being returned, but if you want it to be processed/evaluated you must add a callback function to the $.ajax call, in which you tell it what to do with the response.

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

You can find all the available parameters for jQuery's $.ajax call here.

I also recommend using Firebug (or equivalent, if your not using Firefox) to debug your ajax requests.
It has a console tab in which you can see every XHR request that was made, what were the request/response headers, posted data and response status code.

Also, there is no need for sending the authenticity_token because you are doing a GET and the token is required only on non-GET requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文