Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my jQuery.get() callback being called?

I would expect the following code to display 'hi mom' between the <div id='job-status'></div> on the originating page, but it doesn't:

$(function () {
    function show_status() {
         $.get("<%= status_jobs_path -%>",
               function(data) {
                   $('#job-status').html('hi mom');
               }, 'json');
    }
    show_status();
});

The get() function being triggered: I see the request arrive at my server and a 200 OK response containing my JSON code. But an alert() inside the function(data) { ... } body never gets called, nor is 'hi mom' displayed on the page. However, if I strip the code down to:

$(function () {
    function show_status() {
        $('#job-status').html('hi mom');
    }
    show_status();
});

... then it does display 'hi mom' within the <div id='job-status'></div>.

IASISO (I Am Sure It's Something Obvious), but what am I missing?

like image 783
fearless_fool Avatar asked Dec 30 '11 04:12

fearless_fool


1 Answers

My guess is that you return an invalid json response. Try putting this in your controller action

 render :json => { :message => "Hi mom" }, :status => :ok
like image 141
Cyclonecode Avatar answered Sep 22 '22 23:09

Cyclonecode