Catfish
Catfish

Reputation: 19324

Rails and jQuery - trying to auto update data with setTimeout ajaxily

I have a page that can be accessed in the browser /calendar which points to calendar#index.

I have a javascript setTimeout function which calls the $.get ajax method on the same function because i'm trying to just re-fetch the data with $.get and then update the data on my page with jQuery.

$.get("<%= calendar_path %>", function(data, status) {
    console.log('success');
}, "json").fail(function(data) {
    console.log('fail');
    console.log(data);
});

which calls my calendar#index method

...
respond_to do |format|
    format.js
        format.html # show.html.erb
end

which returns /views/calendar/index.js.erb which contains:

console.log('in js');

which gives this response in firebug console:

GET http://localhost:3000/calendar 304 Not Modified 148ms   
fail
Object { readyState=4, responseText="console.log('in js');", status=200, more...}

The console.log from my index.js.erb file is never logged in the console. Why is this and why does my $.get call always fail?

Upvotes: 0

Views: 554

Answers (1)

Satya Kalluri
Satya Kalluri

Reputation: 5214

The jQuery Ajax request expects the type of response. You have mentioned it as "json". Instead, it should be "script"

Upvotes: 1

Related Questions