4

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
Andrew Latham
  • 5,982
  • 14
  • 47
  • 87

3 Answers3

3

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

c2h5oh
  • 4,492
  • 2
  • 23
  • 30
  • I already discovered this (see edit) and asked a question about it yesterday - http://stackoverflow.com/questions/11195674/running-a-query-and-retrieving-xml-from-an-external-site/11195730#11195730 How can I do the data-pulling? – Andrew Latham Jun 26 '12 at 13:05
  • Write code that will run on your server (same domain) that will get data from the external db (HTTP request by code running on your server instead of user browser) and return it to your ajax request. – c2h5oh Jun 26 '12 at 13:19
1

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.

marteljn
  • 6,446
  • 3
  • 30
  • 43
  • Thank you for pointing that out, but those parts are just for testing, and are just a part of my tests - I also had the state and status written to the console every time the state changed - so the syntax error is not the cause of my problem here. 302 was never written to the console. – Andrew Latham Jun 23 '12 at 14:48
0

Try to do the redirection on the server side

Snapshot from FireBug

See this snapShot

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

Sameh Serag
  • 747
  • 1
  • 8
  • 22