views:

388

answers:

2

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.

+2  A: 

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.

andi
A: 

User POST instead of GET

KoRkOnY