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

3
  • 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. api.jquery.com/jQuery.ajax/#jqXHR Nov 19, 2012 at 20:08
  • possible duplicate stackoverflow.com/questions/4687235/… Nov 19, 2012 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, 2012 at 20:09

1 Answer 1

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.

1
  • great, it works. It is not the same origin policy - I am working with greasemonkey. Thanks for the quick reply.
    – Stasik
    Nov 19, 2012 at 20:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.