如何在执行ajax调用时调试rails(How to debug rails when doing an ajax call)

编程入门 行业动态 更新时间:2024-10-24 14:22:34
如何在执行ajax调用时调试rails(How to debug rails when doing an ajax call)

所以,通常,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 identifier

This 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.

更多推荐

本文发布于:2023-07-22 18:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时调   如何在   ajax   call   rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!