admin管理员组

文章数量:1279085

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?

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?

Share Improve this question asked Dec 30, 2011 at 4:23 fearless_foolfearless_fool 35.2k25 gold badges145 silver badges230 bronze badges 5
  • Do you see the success result in firebug / chrome dev tools? – Adam Rackis Commented Dec 30, 2011 at 4:32
  • 1 Are you sure the response is valid JSON? jQuery will still redirect to the error callback (or deferred.fail for $.get) if parsing fails. – Jonathan Lonowski Commented Dec 30, 2011 at 4:33
  • 1 Yeah, I don't see that acronym catching on. In regards to your question, are you sure the JSON object being returned is valid and properly structured? Also, is this a same-domain request or cross-domain? – Aaron Commented Dec 30, 2011 at 4:33
  • have you tried outputting to the console what the return data contained? – Joseph Commented Dec 30, 2011 at 4:56
  • see below -- the problem was that I was returning mal-formed JSON, which jQuery silently drops. So @jonathan had it right. – fearless_fool Commented Jan 2, 2012 at 5:58
Add a ment  | 

1 Answer 1

Reset to default 12

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

 render :json => { :message => "Hi mom" }, :status => :ok

本文标签: javascriptWhy isn39t my jQueryget() callback being calledStack Overflow