5

I'm working with an older system that is using jQuery 1.2.6. I am sending an AJAX Request via the jQuery.ajax function. The URL that it is hitting is sending a 302 HTTP Redirect response and eventually ends up with a 200 HTTP OK response. I have registered both a success and a complete callback, however neither of them are called.

My question is: Are there any callbacks that can/will be called after a redirect occurs?

jQuery.ajax({
    type: "GET",
    url: url,
    data: null,
    dataType: "json",
    async: false,
    success: function(data, textStatus) {
        alert("SUCCESS CALLED: " + textStatus);
    },      
    complete: function(xhr, status) {
        alert("COMPLETE CALLED");
    }
});

NOTE: The response is HTML, not JSON, however changing the dataType to html alters the request. It sends an OPTIONS request instead of a GET request, and it also no longer redirects. I need these redirects to occur.

John
  • 9,254
  • 12
  • 54
  • 75
  • do you have access to the server code? The browser handles 302 responses transparently, but your success function should eventually be called when the 200 status code is received. – Ben Jun 06 '11 at 15:33
  • Hi Ben, thanks for the help. I do not have access to server code. Regarding the `success` callback, I have an `alert` call in there as a simple test. Following the XHR requests via Firebug, once the 200 is hit, no alert dialog is displayed. – John Jun 06 '11 at 15:36
  • can you post some code? Is the response data been sent from the server the same as the data type specified in your ajax call? – Ben Jun 06 '11 at 15:43
  • Added code above. The response is HTML, not JSON. Changing the `dataType` to `html` alters the request. It sends an `OPTIONS` request instead of a `GET` request, and it also no longer redirects. – John Jun 06 '11 at 17:00

2 Answers2

2

As per the comments:

Changing the dataType to html alters the request. It sends an OPTIONS request instead of a GET request, and it also no longer redirects.

This will happen when you send a crossdomain request. I.e., the url does not point to the same domain as where this script is been served from. This namely violates the Same Origin Policy. Not having access to the server code also confirms that it runs on a different domain. The HTTP OPTIONS request sent by jQuery should let the server return a response with an Allow header with a list of request methods which are allowed to be used on the particular URL so that jQuery could then continue accordingly. This is apparently not happening.

If the server is really completely out your control, you'd need to create a proxy on your server which connects the desired URL and streams its response back. Then alter the jQuery url to point to the proxy URL instead. It's unclear what programming language you're using, but your question history suggests that you're familiar with Java, in that case you can use a simple servlet with java.net.URLConnection or Apache HttpComponents Client for this.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the information. This leads us back to the original question. Given the code above, using `json` as the `dataType` and a `GET` request, are there any callbacks that can/will be called after a redirect? – John Jun 07 '11 at 13:44
  • 1
    To confirm, does the URL point to a different domain? If yes, then you need a proxy on your own domain. If not, then I don't have a feasible explanation for the problem you're seeing. It must have been a crossdomain request somehow. On requests on own domain, jQuery/XHR will just handle it transparently. – BalusC Jun 07 '11 at 13:47
  • Thanks for the help, BalusC. Yes, it does point to a different domain. – John Jul 22 '11 at 13:21
0

Use jquery ajax option

dataType: 'jsonp'

to perform the redirect for cross-domain requests. (Was tested in FF/Chrome browsers)

But this resolution is useful only if you are not handle the response or if the cross-domain-url supports jsonp approach.

katrin
  • 1,146
  • 1
  • 13
  • 24