简体   繁体   English

为什么不调用我的jQuery.get()回调?

[英]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: 我希望以下代码在原始页面上的<div id='job-status'></div>之间显示“hi mom”,但它不会:

$(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. 触发了get()函数:我看到请求到达我的服务器,并且包含我的JSON代码的200 OK响应。 But an alert() inside the function(data) { ... } body never gets called, nor is 'hi mom' displayed on the page. 但是function(data) { ... }体内的alert()永远不会被调用,页面上也不会显示“hi mom”。 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> . ...然后它会在<div id='job-status'></div>中显示'hi mom'。

IASISO (I Am Sure It's Something Obvious), but what am I missing? IASISO(我确定它显而易见),但我错过了什么?

My guess is that you return an invalid json response. 我的猜测是你返回一个无效的json响应。 Try putting this in your controller action 尝试将其放入控制器操作中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM