Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

ajax - How to view the last GET HTTP request in JavaScript

How can I view the last GET http request in JavaScript? Basically what I am after is what I can see my firebug console. When XMLHttpRequests are showing in console I see a line that looks something like:

GET   http://www.domain.com/php/file.php?q0&c=1   200   OK   163ms

How do I view that URL in JavaScript?

EDIT: Just to be clear I'm looking for the URL between GET... and ...200. I don't care about anything else. I don't want any of the other info.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may want to modify the XMLHttpRequest.prototype.open to decorate it with your "tracking" code. Something like this:

var ajaxCalls = [];

XMLHttpRequest.prototype._originalOpen = XMLHttpRequest.prototype.open;

XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
   ajaxCalls.push(url);
   this._originalOpen(method, url, async, user, password);
}

The ajaxCalls array will get populated with the URLs of your Ajax requests. The last request will be at ajaxCalls[ajaxCalls.length - 1].

Note that if you want to track only GET requests, you can simply log URLs only if method === 'GET' in the open() method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...