views:

597

answers:

3

Hi guys,

I'm feeling completely stupid because I've spent two hours solving task which should be very simple and which I solved many times before. But now I'm not even sure in which direction to dig.

I fail to fetch static content using ajax from local servers (Apache and Mongrel). I get responses 200 and 206 (depending on the server), empty response text (although Content-Length header is always correct), firebug shows request in red.

Javascript is very generic, I'm getting same results even here: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first (just change document location to 'http://localhost:3000/whatever') So, it's probably not the cause.

Well, now I'm out of ideas. I can also post http headers, if it'll help.

Thanks!

Response Headers
Connection  close
Date    Sat, 01 May 2010 21:05:23 GMT
Last-Modified   Sun, 18 Apr 2010 19:33:26 GMT
Content-Type    text/html
Content-Length  7466

Request Headers
Host    localhost:3000
User-Agent  Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Referer http://www.w3schools.com/ajax/tryit_view.asp
Origin  http://www.w3schools.com

Response Headers
Date    Sat, 01 May 2010 21:54:59 GMT
Server  Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8l DAV/2 mod_jk/1.2.28
Etag    "3d5cbdb-fb4-4819c460d4a40"
Accept-Ranges   bytes
Content-Length  4020
Cache-Control   max-age=7200, public, proxy-revalidate
Expires Sat, 01 May 2010 23:54:59 GMT
Content-Range   bytes 0-4019/4020
Keep-Alive  timeout=5, max=100
Connection  Keep-Alive
Content-Type    application/javascript

Request Headers
Host    localhost
User-Agent  Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Origin  null

UPDATED:
I've found a problem, it was about cross-domain requests. I knew that there are restrictions, but thought they're relaxed for local filesystem and local servers. (and expected more descriptive error message, anyway)
Thanks everybody!

+1  A: 

It seems lake a caching problem only. Just delete cache inside of Internet Explorer and repeat your experiment. All HTTP GET requests will be cached. IE cache also ajax responses. If you don't like it you can append the URL with the text like '?p=blala'. Where 'p' is a name which will be interpret as a name of a parameter and text 'blala' must be unique in every request. Typically one use (new Date).getTime() construct to generate such 'blala'. IE will "think", that URL is new and will always send request to server.

UPDATED: Caching of static data will be made in all browsers, especially if Web server explicitly allow this: see Cache-Control: max-age=7200, public, proxy-revalidate is the response from server. Just try to go to http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first and modify the line

xmlhttp.open("GET","ajax_info.txt",true);

to

xmlhttp.open("GET","ajax_info.txt?p=" + (new Date).getTime(),true);

then click on "Edit and Click Me >>". Then if you click on "Change Content" button you will see in the HTTP traffic full data contain. I don't see any time 206 code. What it means if Response has "Accept-Ranges: bytes" and "Content-Range" like "bytes 0-4019/4020" inside of HTTP header you can read on http://benramsey.com/archives/206-partial-content-and-range-requests/

Oleg
Forgot to say, I'm using Firefox and Chrome for testing. Anyway, clearing cache really helps to transform 206 to 200, thanks for that! Although, it still doesn't work right.
Nikita Rybak
#for update#It does remove 206, as I said, but response body is still empty (and request is in red in firebug, I wonder if it can tell more specific reason).But thanks for trying.
Nikita Rybak
I had no time this effect with http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first. If your server are accessed from the internet could you post a link. Or you have anther way, how one can reproduce effect which you has.
Oleg
I've posted an update, but thanks for the ideas anyway!
Nikita Rybak
A: 

Found out problem just by lucky guess: see description.
Thanks!

Nikita Rybak
A: 

Hi Nikita Rybak,

I am facing similar problem. from my application, i am sending GET request for an xml file to a UPNP device(a router). But response which is a INputStream is of size 0.

Code used is:

            URLConnection uc = descriptionURL.openConnection();
            uc.connect();
            if (uc instanceof HttpURLConnection) {
                HttpURLConnection huc = (HttpURLConnection)uc;
                int statusCode = huc.getResponseCode();
                String statusMsg = huc.getResponseMessage();

                 if (logger.isDebugEnabled()) {
                    logger.debug("code="+statusCode+" msg="+statusMsg);

                    int i = 1;
                    String name = uc.getHeaderFieldKey(i);

                    while (name != null) {

                        if (logger.isDebugEnabled()) {
                            logger.debug("   " + name + ": " + uc.getHeaderField(name));
                        }

                        name = uc.getHeaderFieldKey(++i);
                    }
                }

                if (statusCode != 200) {
                    throw new IOException("code="+statusCode+" msg="+statusMsg);
                }
            } // end if
            documentStream = uc.getInputStream();

received InputStream size is 0 here. And headerInfo received is:

code=200 msg=OK
875247 [t Delivery Queue] DEBUG DeviceParser           -    CONTENT-LENGTH: 6263
875247 [t Delivery Queue] DEBUG DeviceParser           -    CONTENT-TYPE: text/xml; charset="utf-8"
875247 [t Delivery Queue] DEBUG DeviceParser           -    LAST-MODIFIED: Wed, 01 Jan 2003 01:01:18 GMT
875247 [t Delivery Queue] DEBUG DeviceParser           -    DATE: Wed, 01 Jan 2003 01:01:18 GMT
875247 [t Delivery Queue] DEBUG DeviceParser           -    SERVER: OpenRG/4.0.21.3.3.1.32.1.1.1.6 UPnP/1.0 SAGEM_LB/RG_VERSION
875257 [t Delivery Queue] DEBUG DeviceParser           -    CONNECTION: close
875257 [t Delivery Queue] INFO  DeviceParser           - documentStream : espial.net.misc.c@5dbaf9b size=0

Please let me know you have any workaround or solution or reason for the same. port at which my application listen response, is getting valid xml. But this response is not recieved at application layer.

Your help is most appreciated.

Thanks Rashmi

You're better to post it as a separate question (at least, you'll get some attention). Personally, I'll be of little help for you with connections in Java, as my question was regarding Javascript.
Nikita Rybak