1

I am trying to get the content of a 404 custom page via ajax (Ii need a counter value at this page using Greasemonkey).

Unfortunately jQuery's .fail method of ajax does not give a possibility to actually read the contents of the page like the data value on success.

Is there any workaround? I will also buy vanilla js.

Best rehards

Stasik
  • 2,568
  • 1
  • 25
  • 44
  • The `jqXHR` object returned by the call to the ajax method should have the full response in it. This should be passed in as the first parameter to the `error` method defined on the request. http://api.jquery.com/jQuery.ajax/#jqXHR – Quintin Robinson Nov 19 '12 at 20:08
  • possible duplicate http://stackoverflow.com/questions/4687235/jquery-get-or-post-to-catch-page-load-error-eg-404 – Cyclonecode Nov 19 '12 at 20:08
  • i an writing a greasemonkey script, and suspect that the owner of original site is trying to prevent me from the requests, or maybe it is a bug – Stasik Nov 19 '12 at 20:09

1 Answers1

1

You may do this in Vanilla JS :

var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
    if (httpRequest.readyState === 4) {
        console.log(httpRequest.responseText);
    }
};
pmc.loading.start();
httpRequest.open('GET', url);
httpRequest.send();

Ajax's error callback can be used too :

$.ajax({
    url: url, 
    error: function(httpRequest){
        console.log(httpRequest.responseText);
    }
});

This being said, I wonder, from your comments, if you're not subject to problems related to same origin policy : you can't read in javascript the content of a page issued from another domain if the site's owner didn't put the right headers.

If that's the case, you can't do anything purely client-side without the consent of the site owner. The easiest would be to add a proxy on your site to serve the page as if it was coming from your site.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • great, it works. It is not the same origin policy - I am working with greasemonkey. Thanks for the quick reply. – Stasik Nov 19 '12 at 20:17