找不到HTTP / 1.1 404 - 对Rails路由的jQuery POST请求

时间:2014-11-08 01:45:08

标签: jquery ruby-on-rails post http-status-code-404

我在Ruby on Rails 4应用程序中有一个自定义路由,它通过onChange()函数处理通过jQuery发送的post请求。

目标网址是    http://0.0.0.0:3000/application/get_programs_for_center

这是我的Rails控制器,它处理POST请求

 post '/application/get_programs_for_center', to: 'school_applications#get_programs_for_center'

这是调用此路由的jQuery:

$("#school_application_fls_center").change(function(){
var center_id = document.getElementById("school_application_fls_center").value;
var formdata = {center: center_id}; 
$.ajax({
    url: "/application/get_programs_for_center",
    type: "POST",
    datatype: 'json',
    data: formdata,
    success: function(response){
      var options = $("#school_application_program"); 
      removeOptions(document.getElementById("school_application_program"));
      $.each(response.programs, function(i,item) {
        options.append($("<option />").val(response.programs[i].id).text(response.programs[i].name)); 
      });
    }
});
 });

它目前在Chrome中完美运行,但在Safari,Firefox和Opera中我收到了404错误:

enter image description here

这是我的控制器代码:

def get_programs_for_center
 respond_to do |format|
  @school_application = SchoolApplication.find(session[:current_app_id])
  @select_program = @school_application.program
  center = params[:center]
  programs = Program.where(fls_center_id: center)
  msg = { :programs => programs, :message => "hurray", :program => @select_program} 
  format.json  { render :json => msg } 
 end 

1 个答案:

答案 0 :(得分:0)

所以我在我的rails服务器日志中遇到以下错误,这就是抛出404:

ActiveRecord::RecordNotFound (Couldn't find SchoolApplication without an ID):
app/controllers/school_applications_controller.rb:144:in `block in get_housing_options_for_center'
app/controllers/school_applications_controller.rb:137:in `get_housing_options_for_center'

之后被抛出,因为我试图抓住一个未设置的会话变量(因此它不能用于所有其他浏览器与Chrome分离,其中会话var存储为我一直在做的我通过浏览器开发的大部分内容。)

 @school_application = SchoolApplication.find(session[:current_app_id])

因为没有设置变量而引发错误所以我用它替换了它:

  if session[:current_app_id]
    @school_application = SchoolApplication.find(session[:current_app_id])
  else
    @school_application = SchoolApplication.new() 
    session[:current_app_id] = @school_application.id
  end

我不再获得404.