javascript 如何调试 Ajax 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5618668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:47:36  来源:igfitidea点击:

How to debug Ajax request

javascriptruby-on-railsajaxfirebug

提问by demas

I have a Rails application which generate HTML like this:

我有一个 Rails 应用程序,它生成这样的 HTML:

<a href="/ReadItLater/stock?qid=5618572&amp" data-remote="true">stock it</a>

When I click on this link in browser I can get information about AJAX request on Firebug, on Console tab. I can see the respond of the request:

当我在浏览器中单击此链接时,我可以在控制台选项卡上的 Firebug 上获取有关 AJAX 请求的信息。我可以看到请求的响应:

$("#stock_5618528").hide();

enter image description here

在此处输入图片说明

But how can I set breakpoint on this line and debug this code?

但是如何在这一行上设置断点并调试这段代码?

回答by Richard Dalton

If you change your response to include the debugger keyword it should hit that as a breakpoint. So in this case the response would be:

如果您更改您的响应以包含调试器关键字,它应该将其作为断点。所以在这种情况下,响应将是:

debugger;
$("#stock_5618528").hide();

Obviously don't forget to remove that when it goes live. :D

显然不要忘记在它上线时将其删除。:D

回答by Robin Maben

Also, since you're using firebug.

另外,因为您使用的是萤火虫。

Try using using `console.log()'. It is extremely handy.

尝试使用`console.log()'。它非常方便。

You can view your output after your script has executed rather than interrupting execution and then having to deal with ajax timeouts.

您可以在脚本执行后查看输出,而不是中断执行然后不得不处理 ajax 超时。

All you might need to do is..

您可能需要做的就是..

$.ajax({
  url: "...",
  success: function(response){
    console.log(response);
  }
});

回答by Syed Ali

InfernalBadger responded with a solution for front-end/script debugging. If you're interested in debugging at the server side (ie in the Rails code), do the following

InfernalBadger 提供了一个前端/脚本调试解决方案。如果您对在服务器端(即在 Rails 代码中)进行调试感兴趣,请执行以下操作

  1. Start webrick with:
  1. 使用以下命令启动 webrick:
rails server --debugger
rails server --debugger
  1. And add debugger in the rails code/view where you'd like it to breakpoint, reaching which you'll get a console with all the environment and context loaded up!
  1. 并在 Rails 代码/视图中添加调试器,您希望它在断点处设置断点,到达该断点后,您将获得一个加载了所有环境和上下文的控制台!
<% debugger %>
<% debugger %>