[英]HTTP/1.1 404 Not Found - jQuery POST request to Rails route
I have a custom route, in my Ruby on Rails 4 application, which handles post requests sent via jQuery through an onChange()
function. 我在Ruby on Rails 4应用程序中有一条自定义路由,该路由处理通过
onChange()
函数通过jQuery发送的发布请求。
The destination URL is http://0.0.0.0:3000/application/get_programs_for_center
目标URL是
http://0.0.0.0:3000/application/get_programs_for_center
Here is my Rails controller which handles the POST request 这是我的Rails控制器,它处理POST请求
post '/application/get_programs_for_center', to: 'school_applications#get_programs_for_center'
And here is the jQuery which calls this route: 这是调用此路由的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));
});
}
});
});
It currently is working perfectly in Chrome, however in Safari, Firefox and Opera I am getting this 404 error: 目前,它在Chrome浏览器中运行正常,但是在Safari,Firefox和Opera中,我遇到了404错误:
Here is my controller code: 这是我的控制器代码:
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
end 结束
So I was getting the following error in my rails Server log, which was throwing the 404: 所以我在Rails Server日志中遇到了以下错误,该错误引发了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'
And then was thrown because I was trying to grab a session var that hadn't be set (hence why it didn't work on all the other browsers seperate from Chrome, which had the session var stored as I had been doing most of my development through that browser). 然后被抛出,是因为我试图获取一个未设置的会话变量(因此为什么它不能在与Chrome分开的所有其他浏览器上运行,因为Chrome浏览器在我大多数时候都存储了会话变量通过该浏览器进行开发)。
@school_application = SchoolApplication.find(session[:current_app_id])
so that threw an error as the variable hadn't been set so I replaced it with this: 由于未设置变量,因此抛出了错误,因此我将其替换为:
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
and I no longer get a 404. 我不再得到404。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.