所以,通常,rails错误并给我一个很好的错误页面来读取。 还有一个可以调试的控制台。
但是,当做这样的ajax调用时:
$('.the-button').mousedown(function(ev) { var returned_stand_id = 1 $.ajax({url: "/stands/create", type: "POST", contentType: "application/json", dataType: "json", data: '{"the_name": "' + returned_stand_id + '"}', success: function(data){ returned_the_id = data.the.id; window.location = '/s/' + returned_the_id; } }); });我在控制台上遇到chrome错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)我的问题不是这个例子中的问题。
我的问题是在测试/调试ajax调用时如何显示有用的rails错误消息。
编辑1:我补充说:
... window.location = '/s/' + returned_the_id; } error: function(jqXHR, textStatus, errorThrown ){ console.log("jqXHR = "+JSON.stringify(jqXHR)); console.log("textStatus = "+JSON.stringify(textStatus)); console.log("errorThrown = "+JSON.stringify(errorThrown)); } ...但是,这只是导致JS无法加载错误:
Uncaught SyntaxError: Unexpected identifier此错误特别针对: error: function(jqXHR, textStatus, errorThrown ){
So, usually, rails errors and gives me a nice error page to read from. There is also a console to debug in.
But, when doing an ajax call like this:
$('.the-button').mousedown(function(ev) { var returned_stand_id = 1 $.ajax({url: "/stands/create", type: "POST", contentType: "application/json", dataType: "json", data: '{"the_name": "' + returned_stand_id + '"}', success: function(data){ returned_the_id = data.the.id; window.location = '/s/' + returned_the_id; } }); });I get a chrome error on the console of:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)My question is not what is the problem in this instance.
My question is how do I display a helpful rails error messages when testing/debuging ajax calls.
EDIT 1: I added:
... window.location = '/s/' + returned_the_id; } error: function(jqXHR, textStatus, errorThrown ){ console.log("jqXHR = "+JSON.stringify(jqXHR)); console.log("textStatus = "+JSON.stringify(textStatus)); console.log("errorThrown = "+JSON.stringify(errorThrown)); } ...But, this just caused the JS to fail to load with an error:
Uncaught SyntaxError: Unexpected identifierThis error is specifically on: error: function(jqXHR, textStatus, errorThrown ){
最满意答案
正如@Sergio所说,请查看rails日志以获取错误的详细信息。 但是,要回答您的问题,您可以通过向ajax调用选项添加error块来输出有关javascript代码中错误的更多信息。
$('.the-button').mousedown(function(ev) { var returned_stand_id = 1 $.ajax({ url: "/stands/create", type: "POST", contentType: "application/json", dataType: "json", data: '{"the_name": "' + returned_stand_id + '"}', success: function(data){ returned_the_id = data.the.id; window.location = '/s/' + returned_the_id; }, error: function(jqXHR, textStatus, errorThrown ){ console.log("jqXHR = "+JSON.stringify(jqXHR)); console.log("textStatus = "+JSON.stringify(textStatus)); console.log("errorThrown = "+JSON.stringify(errorThrown)); } }); });它可能会显示rails stack trace。
As @Sergio says, look in your rails log for details of the error. However, to answer your question, you can output more info about the error in your javascript code by adding an error block to the ajax call options.
$('.the-button').mousedown(function(ev) { var returned_stand_id = 1 $.ajax({ url: "/stands/create", type: "POST", contentType: "application/json", dataType: "json", data: '{"the_name": "' + returned_stand_id + '"}', success: function(data){ returned_the_id = data.the.id; window.location = '/s/' + returned_the_id; }, error: function(jqXHR, textStatus, errorThrown ){ console.log("jqXHR = "+JSON.stringify(jqXHR)); console.log("textStatus = "+JSON.stringify(textStatus)); console.log("errorThrown = "+JSON.stringify(errorThrown)); } }); });It might show the rails stack trace.
更多推荐
发布评论