問題描述
我有一個頁面在提交表單時發送 XHR 請求,我想讓 Chrome 在收到響應時中斷.實現這一點的最佳方法似乎是 Chrome 有一個我可以調用的 javascript 函數來中斷執行,但到目前為止我一直找不到類似的東西.還有其他解決方案嗎?
I have a page that sends an XHR request when a form is submitted and I would like to get Chrome to break when it receives a response. It seems like the best way to accomplish this would be if Chrome has a javascript function that I can call that breaks execution but I've been unable to find anything like that so far. Is there another solution?
編輯:
我實際上沒有為請求定義回調,所以我不能那樣設置斷點.請求正在使用這行 jquery 代碼發送:
I don't actually have a callback defined for the request so I can't set a breakpoint that way. The request is being sent with this line of jquery code:
$.post(this.action, $(this).serialize(), null, "script");
其中 this
是一個表單元素.null
參數是您通常定義回調的地方,但使用 "script"
參數,服務器返回原始 javascript 然后直接執行,所以它似乎是唯一的中斷和單步執行代碼的方法是使用 debugger;
語句.這可行,但是在單步執行代碼時,您實際上無法看到您所在的行,因此有點尷尬.我懷疑這是 Chrome 調試工具的限制.
where this
is a form element. The null
argument is where you would usually define a callback but with the "script"
argument, raw javascript is returned by the server and then directly executed, so it seems the only way to break and step through the code is with the debugger;
statement. This works, but when stepping through the code you can't actually see which line you are on so its a little awkward. I suspect that this is a limitation of Chrome's debugging tools.
推薦答案
下拉 chrome 控制臺 (ctrl+shift+j) 并鍵入以下任意一個:
drop down the chrome console (ctrl+shift+j) and type any of these:
只需重寫jquery ajax:
Just rewrite the jquery ajax:
var prevajax = jQuery.ajax;
jQuery.ajax = function () { debugger; return prevajax.apply(jQuery, arguments); };
或者如果你沒有使用 jQuery,重寫 xhr 類:
or if you are not using jQuery, rewrite the xhr class:
var prevxhr = XMLHttpRequest;
XMLHttpRequest = function (){debugger; prevxhr.apply(this, arguments);};
中斷后,只需按shift+f11,直到找到發起ajax請求的方法.
After it breaks, just press shift+f11 until you find the method which initiates the ajax request.
這篇關于在 Chrome 中的 XHR 中設置斷點的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!