Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out which Javascript causes a jQuery Ajax request?

How to find the Javascript code from where a Ajax request triggered? In Firebug's console we are able to identify the file and line number, but if we're using $.ajax or $.post or some similar function of jQuery, it will only log the jQuery library file in Firebug's console. Is there any way to get the actual trigger point of the jQuery function?

like image 780
Stranger Avatar asked Jun 20 '13 07:06

Stranger


People also ask

How do I know if AJAX request is running jQuery?

You could use ajaxStart and ajaxStop to keep track of when requests are active. Show activity on this post. Show activity on this post. Checking for null to determine if the request object exists is important, but if it is not null you should really check request.

What happens when JavaScript makes an AJAX request in a browser?

When you make an AJAX request, your browser sends an HTTP request to a given address. The server on the other end of the request responds, and returns the data to your browser. This is the same thing that happens when you navigate to a new web page.

Which global event is triggered if an AJAX request?

The ajaxStart and ajaxStop events are events that relate to all Ajax requests together. This event is triggered if an Ajax request is started and no other Ajax requests are currently running.


2 Answers

Use Chrome in the DevTool you have Sources.

If you open this you will see on the right side XHR/fetch Breakpoints, if you check Any XHR your script will pause at every request that uses XMLHttpRequest (so ever request that does not use jsonp for requests).

If the Any XHR options are not available (only No Breakpoints is listed) then you have to click on the + leave the Break when URL contains: field blank and hit enter. This will create the Any XHR option. (Thanks to Yasmin French for this info)

With the Call Stack (also on the right side) you will see what the origin of the request was.

But as I mentioned this does not break on jsonp requests if you want to trace these you need to use the not minified version of jQuery (or include the sourcemap of the minified version) and set a breakpoint in its source at the correct part. To find this part you can use the following steps:

  1. Create a jsonp request in your code and set a breakpoint at this place.
  2. Call this part of your code so that you switch to the debugger.
  3. Use the Step into, now you should be in the jQuery code. If you now place a breakpoint there, Chrome will stop for every jsonp request.

A note: Sometimes Chrome (probably only in the beta or dev versions) tends to lose the breakpoints on reloading, so you need to check if they still exist on reload.

like image 99
t.niese Avatar answered Oct 07 '22 22:10

t.niese


This may not have existed in earlier Chrome versions, but Version 56.0.2924.87 has an 'Initiator' column that tells the html/ js file and exact line within that file that initiated the request.

This request can be XHR, http request for jpg, css or anything else.

Pretty sleek and helpful in tracing back requests.

Here's how to use it?

  1. Press "F12" to open the developer console.
  2. Look for "Initiator" column in each request, you can see "jquery.min.js:4", which means the request was initiated from the 4th line of the file "jquery.min.js".

enter image description here

like image 20
Punit S Avatar answered Oct 07 '22 23:10

Punit S