views:

36

answers:

1

I'm writing a custom web server. When I enter the URL of my server in the browser, I get the sample text I write out to the socket in my browser correctly. This is the HTTP response that I write:

HTTP/1.1 200 OK\r\n
Server: My Server\r\n
Date: Blah\r\n
Content-Type: text/plain\r\n
\r\n
This is some sample text. This appears in my browser correctly when I connect directly to the server entering the URL.

However, if I use Ajax (jQuery) to access the same URL and then set the innerHTML of an element on the HTML page, I do not get any data printed. Is there any other header I need to set on my web server so that the browser's XMLHttpRequest object detects the response?

+1  A: 

Probably needed Content-Type

S.Mark
I tried setting Content-Type: text/plain It still doesn't work.
ErJab
@ErJab, Is that cross-domain?
S.Mark
and Please use firebug to check headers, its properly getting or not.
S.Mark
Yes it is cross-domain, but I take care of it by processing the OPTIONS request sent by the browser. In Firebug, I see the browser first making the OPTIONS request since it is cross-domain and then it makes the GET request. When I examine the response headers, all the headers are set correctly, but the content alone is missing.
ErJab
Just for the note about crosss-domain, its not allowed by default, you need to set `Access-Control-Allow-Origin: *` , and more info at here https://developer.mozilla.org/En/HTTP_access_control#Access-Control-Allow-Origin
S.Mark
Yes yes, I already did that! :) Even after that it doesn't work!
ErJab
Imm, I see. Is your site online, btw?
S.Mark
@S.Mark Thank you so much for the link! It's working now! I saw an example HTTP exchange. So when a cross-domain AJAX request is made, first an OPTIONS header is made. In this I set the Access-Control-Allow-Origin: * and Access-Control-Allow-Methods: POST, GET, OPTIONS. And after that, the actual POST request is triggered. It turns out I need to set the Access-Control-Allow-Origin: * header once again in this POST response for it to work! And I was testing it on two servers on my LAN.Thanks again!
ErJab
@ErJab, Great to know that you got it working, cheers.
S.Mark