Reputation: 747
I'm trying to read the http-response by ajax request. As an echo-server I use a simple socket on my host. It returns http-response with XML in it. Just like this:
HTTP/1.1 200 OK
Date: Fri, 18 Nov 2011 03:16:22 GMT
Content-Length: 94
Connection: close
Content-Type: text/xml
got it. waiting for the next..
As a client I'm using the following ajax request:
<html>
<head>
<script type="text/javascript" src="jquery-1.5.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "http://localhost:8090/",
dataType: "text",
complete: function(jqXHR, textStatus){
alert(textStatus);
alert(jqXHR.responseText);
}
});
});
</script>
</head>
<body>
</body>
</html>
After this script execution I receive the following error in FF:
XML Parsing Error: no element found Location: moz-nullprincipal:{62148931-591e-41d7-8625-c86149386fc4} Line Number 1, Column 1:
I can read the resulting XML without any error if I request it using address bar in FF, Chrome and IE, but this Ajax call returns error. It seems that I'm missing something working with ajax. Can you help me with this? Thank you.
Upvotes: 2
Views: 2468
Reputation: 7844
If I'm reading your question correctly, you're loading some HTML with Open File from a local file, and pointing it to a socket server running on localhost. FireFox will consider these to be different domains. The HTML script needs to come from the same localhost server as the XML being requested.
In other words, for security reasons the browser will only allow AJAX requests to the "same origin". This is a term that (for most browsers) means "same protocol, same host, same port".
http://localhost:8090/ is not the same origin as http://localhost or https://localhost (note the https vs. http), nor is it the same origin as an HTML file loaded off your machine (like file:///my/directory/test.html).
You can try adding the following response header to your XML response:
HTTP/1.1 200 OK
Date: Fri, 18 Nov 2011 03:16:22 GMT
Content-Length: 94
Connection: close
Content-Type: text/xml
Access-Control-Allow-Origin: *
If your browser supports Cross Origin Resource Sharing (CORS) then the above "Access-Control-Allow-Origin" header should allow the XML to be accessed from a different domain.
Upvotes: 2