为什么我的 Rails Ajax 无法正常工作?

可能还是因为我对Ajax了解较少......

我正试图对我的服务器进行连续的Ajax调用,并且为此学习制作了Ademapp。我有以下代码:

job_status/index.html.erb:

<h1>Loading data ......</h1>

<div id = "job-id-container">
    Aditya
</div>


<script type="text/javascript">
    function tick() {
      var ajaxOpts = {
            type: "get",
            url: "/job_statuses",
            dataType: 'json',
            data: "status=reload"

        };
    $.ajax(ajaxOpts);
    setTimeout('tick()', 1000);
}

$(document).ready(function() {
    tick();
});
</script>

job_statuses_controller:

class JobStatusesController < ApplicationController
    def index       
        @job = JobStatus.last       
    end
end

index.js.erb:

$('#job-id-container').append("<%= @job.message %>")

在浏览器控制台中,我得到响应304,而且它也没有更新页面上的数据。

请告诉我哪里出错了,并给出正确的代码建议......多谢!

I have

 dataType: 'json' 

which was excepting data in json and i want to render script. removing that line fixed the problem.

Personally not tested, try smth like this:

<script>
function repeat_ajax_call(){
   $.get("/job_statuses", function(data){
        console.log(data);
        $('#job-id-container').append(data.message);
   }, 'json');
}

$(repeat_ajax_call);
$(document).on("turbolinks:load", repeat_ajax_call);
</script>

controller:

def index       
  @job = JobStatus.last
  respond_to do |format|
    format.html { @job }
    format.json { render json: json_format(@job) }
    #OR
    format.json { render json: {data: @job}}
  end
end