Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the piece of code that initiated ajax request

I have MVC view, in that tens of javascript files are included. there are some ajax requests initiated from the page. I would like to know from which particular js file the call was initiated.

I spent some time exploring Firefox addons - Firebug and HttpFox. Both are showing the http request, header , cookies and other hell lot of information, but nothing about the source where the ajax request was initiated.

like image 777
Brainchild Avatar asked Apr 30 '12 20:04

Brainchild


People also ask

How do you know that an AJAX request has completed?

By determining the readyState property value of XMLHttpReqyest, One can know if the request is completed. If the readyState value = 4, the request has been completed and the data is available.

What kind of information can be received by an AJAX request?

AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.

How do you check AJAX response is HTML or JSON?

jQuery auto-detects the dataType: If the response is JSON, a properly behaving application would set the Content-Type to application/json. So all you have to do, if the server is well-behaving, is to test if the Content-Type header in the response starts with application/json.


2 Answers

I've been finding some of Chromes more recent debugging features very useful. Bring up the inspector(Ctrl+Shift+I) and switch to the Network tab before launching your application or performing the actions in question. Each http request will show the origin of the request in the Initiator column. If javascript initiated the request, it will show you the filename and line number with a clickable link to go straight to it.

like image 123
DannyMeister Avatar answered Oct 24 '22 18:10

DannyMeister


Another very good option in chrome developer tools is XHR Breakpoint. Press F12, Select the sources tab and on the right had side under XHR Breakpoint add a new conditional breakpoint condition by specifying the text you expect to find in the URL on which the ajax call is being made. It will stop on any ajax call which contains the text specified in the URL. You can give it a try on this URL (look for the heading Breakpoints on XHR)

like image 21
DotNetJourneyMen Avatar answered Oct 24 '22 20:10

DotNetJourneyMen