【问题标题】:debugging jquery ajax script with runtime error调试带有运行时错误的jquery ajax脚本
【发布时间】:2016-02-16 16:57:58
【问题描述】:

如何从收到的 jQuery AJAX 脚本中查看运行时错误?

我正在编写一个嵌入其特定 Web 服务器的(免费软件)应用程序(MELT 监视器,在 GNU/Linux/Debian/x86-64 上)。有关详细信息,请参阅this question。我正在提交d624e22497de...

我在 Linux/Debian/Gnu/x86-64 上使用 Firefox 38 或 42 以及最近的 Firebug。

我正在使用带有脚本的 jQuery $.ajax

$.ajax
({url: "/canvasedit",
  method: "POST",
  data: {"do_fillcanvas": true},
  dataType: "script",
  success: ajaxcanvascript
 });

ajaxcanvascript 被正确调用(并显示了console.log),因此从 HTTP 的角度来看,AJAX 请求已成功(200 OK)。

但是,当do_fillcanvas 收到的 AJAX 脚本中出现运行时错误时,这是​​一个类似于以下内容的生成脚本:

     console.log('dofillcanvas canvedit.c:206 siz 1; this=', this,
        ' momc_display_canvas=',momc_display_canvas);
 momc_display_canvas('09:26:39',[
     momc_top_entry(momc_item_ref('notice'),
           momc_node(momc_item_ref('comment'),
                 [
                 momc_string("some simple notice"),
                 momc_node(momc_item_ref('web_state'),
                       [
                           momc_int(2)]),
                 momc_item_val('hashset'), momc_set([
                     momc_item_ref('canvasedit'),
                     momc_item_ref('microedit'), momc_item_ref('the_agenda')])
                 ,
                 momc_tuple([
                     momc_item_ref('web_session'), momc_nil_ref(), 
                     momc_item_ref('the_system')])
                 ]))]);

 console.log('dofillcanvas canvedit.c:239 this=', this);
 addupdatehtml('displayed <tt>Sat Nov  7 09:26:39 2015 MET</tt>');
 console.log('dofillcanvas canvedit.c:252 done 09:26:39.11 this=', this);

我的 Firebug 控制台中没有任何错误消息(或者从 Firefox 中看不到任何内容)。

我正在调试的运行时错误是例如一些未定义的方法从momc_display_canvas 调用深度(使用jcanvas,我不太熟悉)。

那么,如何从接收到的 jQuery AJAX 脚本中轻松查看运行时错误?

我到处添加console.log,但这不方便...

【问题讨论】:

  • 检查控制台的网络选项卡以查找请求的状态。
  • 我个人会开始逐行注释,直到错误消失。
  • 绑定窗口onerror事件?请参阅例如 stackoverflow.com/a/10556743/1414562 不确定这是您要查找的内容,但听起来像
  • @Taplar:大部分行都是生成的,也就是说在生成器中工作
  • @A.Wolff:可悲的是,它看起来对我不起作用......

标签: jquery ajax linux firefox firebug


【解决方案1】:

如果我是对的,那么您要求在将 ajax 调用到 ajax 完成时处理错误。 jquery ajax 有一个错误的参数回调函数,您可以使用该回调来处理您的错误。

$.ajax({
...
error: function(error){
console.log(error);
}
...
})

如果你想处理全部错误,你可以使用 try catch 块包裹整个 ajax 调用。希望这会对你有所帮助 })

【讨论】:

  • 我以为$.ajax中的error是针对协议相关的错误,这里AJAX的HTTP请求是成功的...
猜你喜欢
  • 2013-10-06
  • 2013-07-17
  • 2018-08-13
  • 1970-01-01
  • 2012-05-10
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多