9

is it possible with javascript to listen for and capture outgoing HTTP requests? (For example, AJAX calls). Sort of like firebug, etc

3
  • To clarify, what I want to do is run a website in an iframe and have my JS listen for outgoing http requests that are sent through my browser. Commented Jan 12, 2011 at 7:01
  • You're not going to be able to monitor the whole browser using content JavaScript. That would violate the same-origin policy. To do that, you have to run inside the browser's chrome (including as an extension). Commented Jan 12, 2011 at 7:11
  • What access does an extension give you that you can't do via straight javascript? Thanks :) Commented Jan 12, 2011 at 17:15

3 Answers 3

6

Nope, not like firebug. The closest thing you can do, if you're using a framework such as jQuery or Microsoft AJAX.NET, you can be notified when an AJAX request is issued through that specific framework.

For jQuery, that's .ajaxStart()

There are heaps of requests that this won't catch, though, such as all requested assets (script files, images, etc.)

Sign up to request clarification or add additional context in comments.

3 Comments

This feels like the more correct answer to me: especially if you take a security oriented mindset.
David, maybe you could help to answer: stackoverflow.com/questions/18461821
This is the correct answer to the question.
5

Sure, you can trace AJAX requests on your own pages. Make a wrapper for the XMLHttpRequest constructor that records the information you're interested in.

EDIT: An extension lets you observe internal browser operations. For example, you can see in Firebug's source that it observes all of Firefox's HTTP topics. These are "http-on-modify-request", "http-on-examine-response", and "http-on-examine-cached-response."

Comments

1

If you want to capture all HTTP requests, I would suggest using Charles. Its an awesome tool which works as proxy and gives result in human readable form.

Shows Request, Response, Time Chart. The request is shown with headers. Response is shown with headers along with content. Worth trying it.

1 Comment

This doesn't answer the question. He wants to know how to do this from javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.