将jquery错误发布到服务器时出错?

时间:2017-10-11 20:12:44

标签: javascript jquery

我在应用程序中到处使用jQuery ajax POST。以下是ajax帖子的示例。内容类型基于application/json; charset=utf-8设置。内容类型可以是$.ajax({ type: "POST", data: somedata, url: 'someurl', contentType: 'somecontenttype', processData: true, cache: true, }) .done(function (response, textStatus, jqXHR) { //dosomething here }) .fail(function (jqXHR, textStatus, errorThrown) { logger.log({ message: jqXHR, code: "Error" }); }) fail

jqXHR

上述方法运行正常。

但是,出于任何原因,如果POST失败,我想将该错误记录到服务器。所以在POST处理程序中我将var logger = (function ($) { var module = {}; module.log = function (obj) { try { $.ajax({ type: "POST", data: obj, url: '/jslog', contentType: 'application/json; charset=utf-8' }) .fail(function (jqXHR, textStatus, errorThrown) { console.error('JS error report submission failed.'); }) } catch (ex) { console.error('JS error report submission failed.'); } } return module; })(jQuery); 记录到服务器。我创建了一个try-catch对象服务器的记录器。如下所示

logger

请注意,代码包含在stringify内,因此如果记录到服务器失败,我们只需在控制台中记录一般错误。

问题
module.log = function (obj) { try { $.ajax({ type: "POST", data: { log: JSON.stringify(obj) }, url: '/jslog' }) .done(function (data, textStatus, jqXHR) { }) .fail(function (jqXHR, textStatus, errorThrown) { console.error('JS error report submission failed.'); }) } catch (ex) { console.error('JS error report submission failed.'); } 尝试将obj POST到服务器时。我得到以下异常。在实际POST之前发生错误,因此请求永远不会到达服务器。

  

无法获取未定义或空引用的属性'toLowerCase

     

“TypeError:无法获取未定义的属性'toLowerCase'或null   在jqXHR.getResponseHeader上引用\ n   (http://localhost:58641/Scripts/jquery-1.12.4.js:9523:7)\ n添加   (http://localhost:58641/Scripts/jquery-1.12.4.js:10073:4)\ n at   buildParams   (http://localhost:58641/Scripts/jquery-1.12.4.js:10061:3)\ n at   buildParams   (http://localhost:58641/Scripts/jquery-1.12.4.js:10055:4)\ n at   jQuery.param   (http://localhost:58641/Scripts/jquery-1.12.4.js:10095:4)\ n在ajax   (http://localhost:58641/Scripts/jquery-1.12.4.js:9612:4)\ n at   module.log(http://localhost:58641/Scripts/logger.js:5:13)\ n at   module.showError(http://localhost:58641/Scripts/common.js:164:13)\ n
  在匿名功能   (http://localhost:58641/Scripts/common.js:322:13)\ n着火了   (http://localhost:58641/Scripts/jquery-1.12.4.js:3232:6)“

UPDATE1
我将记录器更改为POST对象作为字符串。所以我在POST期间A potentially dangerous Request.Form value was detected from the client客户端上的对象,并删除内容类型

jqXHR.responseText

将字符串POST到服务器,但服务器拒绝其错误 html 那是因为 awk '{ sub(/C[0-9]+\w/,"",$2); print $1,$2 }' f1 host1 5 host2 55 host15 10

服务器端代码是ASP.NET MVC

1 个答案:

答案 0 :(得分:0)

jQuery ajax方法在安排提交数据时会询问对象,并且当它通过成员工作时,将调用函数来获取值:

jqXHR

因此,如果您发送的对象也有jqXHR对象作为成员或getResponseHeader对象本身,它最终会遇到Unable to get property toLowerCase of undefined or null成员并尝试调用它而没有参数导致console.log(loc);错误。

评论中提到的最佳选择是将提交的对象字符串化为记录器。

相关问题