AJAX not handling 302 redirect

2019-06-26 11:26发布

问题:

I have a database on an external server that I am trying to query. To do this, I am going on my local server (Tomcat) and creating an AJAX call (just the XMLHttpRequest object - I am not using any JavaScript libraries) to the page with a query appended. Pasting the exact same URL into Firefox causes it to try to download an XML document. My goal is to use AJAX to get that XML document.

The problem I am having is that when I make the call with AJAX, Firebug shows that the GET response returned 302 "Moved Temporarily" with a red X next to it. The header for the GET response has a Location parameter with OAuth authorization, and when I copy and paste the location parameter it takes me to the correct page (tells me to download the XML object).

EDIT: I tried it using jQuery's $.get("URL", function(data){alert(data)}); and the same thing happened - no alert, but a red GET request and 302 in Firebug.

Based on this information, I think that the database I am calling is first trying to redirect me to some OAuth thing, which then returns an authorized URL with which to access the database. This is what I should use to call the database, get the XML object back, and then do my thing. AJAX doesn't seem to be able to handle the redirect and is instead crashing.

I'm not sure this is correct, however, because I tried using the following code:

  else if (xmlhttp.readyState == 4 && xmlhttp.status == 302){
  alert("Hello 302!");
}
else {
  document.getElementById("test").innerHTML = "On state: " + xmlhttp.readyState + "<br />HTTP Status: " + xmlhttp.status;
}

and it didn't give me an alert - instead it shows that it is on state 4 and status 0. I don't understand why it would return status 0. (Edit: Fixed the typo mentioned in answer 1 and nothing changed)

So my questions are:

  • What, exactly, is going on here?
  • What is the 0 status, why is Firebug giving me an X next to 302 in the console, and why isn't there a redirect?
  • How can I fix this?
  • Once I do fix it, will I be able to grab that XML file, or is there something else I need to do?

EDIT WITH UPDATE: It's a cross-site scripting issue. I went on the external server and ran the exact same script and was able to retrieve and parse an XML document containing the result of the query. The only obstacle is figuring out how to do this from an external server. I have access to the configuration of the external server and will be researching how to manipulate it to allow access via database queries from other sites.

回答1:

Since it's an ajax request you can't pull data from another domain: http://en.wikipedia.org/wiki/Same_origin_policy

All you can do here really is request data from your own server (same domain) and have it pull data from the external db for you.

edit: this response is over 3 years old and now with modern browsers (not IE < 10) you can use Cross Origin Resource Sharing - https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS



回答2:

You have a syntax error readystate needs to be readyState. The way it is written, it will never be 4.

Another piece of advice would be to just check for a readyState of 4 and within that statement test for the status of 302. That way you will be able to troubleshoot whether or not it is the 302 that is causing your issue.



回答3:

Try to do the redirection on the server side

Snapshot from FireBug

In this snapshot the Ajax request sent to server side (where there is the redirection)