2

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.

3
  • Are you requesting the XML from the same domain the client script is coming from? Also, try a dataType of "xml", not "text". Commented Nov 18, 2011 at 3:32
  • yes, all activity runs on my local host. Change from "text" to "xml" 'dataType' value didn't help( Commented Nov 18, 2011 at 3:38
  • "local host" is not the same thing as same domain; see my answer below. Commented Nov 18, 2011 at 3:42

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What are you trying to accomplish? If you're just playing around to try out AJAX, use a real HTTP server (like Tomcat or Apache) so you can serve both items from the same place. Otherwise you need to do things like JSONP or a script proxy, neither of which allow you to send raw XML.
Great! You suggestion about additional header resolved my issue :) Actually I'm trying to simulate the communication process between my C++ application and some web-application running somewhere in browser context. Big thank you for your help! :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.