XHR Problem on all Browsers -- except IE

M

Mel Smith

Hi:

I've been working with XHR for a couple of months now, and enjoying the
hi-speed interaction between my IE7 browser, and my CGI server.

This morning I decided to test XHR on Chrome, FF, and Safari:

Results:

1. The 'post' request was sent to my server properly.

2. My CGI app sent the same text response I've been sending for a month
now.

3. The response text was apparently not received --- only a status of
zero (0) was returned

So, for IE7 (and IE 6), the reponse came quickly and correctly:

Ready State: 4, and Status: 200

But for all the other browsers I tested, I get:

Ready State: 4, and Status: 0 !!!

So, apparently, the other browsers are getting a response but setting
the status to 0 -- which means they found something wrong with the response
which IE found OK ???

Can anybody help me with this please ??

I'll place the relevant section of XHR code below:

**************************************
HTTP.post = function(url, values, callback) {
var request = HTTP.newRequest();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200 ) {
callback(request.responseText); // get here correctly
with IE
}
else {
callback(request.status); // Get here with other browsers
and a status of 0.
}
}
}

request.open("POST", url);
// This header tells the server how to interpret the body of the request
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
// Encode the properties of the values object and send them as
// the body of the request.
request.send(HTTP.encodeFormData(values));
if (clbplr == "clb") clbrequest=request; // set global vrbl clbrequest
if (clbplr == "plr") plrrequest=request; // set global vrbl plrrequest
};

*********************************

Thanks for some guidance here about using XHR with Chrome,FF, and Safari
 
M

Martin Honnen

Mel said:
I've been working with XHR for a couple of months now, and enjoying the
hi-speed interaction between my IE7 browser, and my CGI server.

This morning I decided to test XHR on Chrome, FF, and Safari:

Results:

1. The 'post' request was sent to my server properly.

2. My CGI app sent the same text response I've been sending for a month
now.

3. The response text was apparently not received --- only a status of
zero (0) was returned

So, for IE7 (and IE 6), the reponse came quickly and correctly:

Ready State: 4, and Status: 200

But for all the other browsers I tested, I get:

Ready State: 4, and Status: 0 !!!

So, apparently, the other browsers are getting a response but setting
the status to 0 -- which means they found something wrong with the response
which IE found OK ???

Can anybody help me with this please ??

Do you have a public URL where we can visit the problem?
I'll place the relevant section of XHR code below:

**************************************
HTTP.post = function(url, values, callback) {
var request = HTTP.newRequest();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200 ) {
callback(request.responseText); // get here correctly
with IE
}
else {
callback(request.status); // Get here with other browsers
and a status of 0.
}

Have you checked what statusText shows, what responseText shows?
 
D

Denis McMahon

I'll place the relevant section of XHR code below:

**************************************
HTTP.post = function(url, values, callback) {

where's the "HTTP = <something>" line?

Because I suspect that what may be happening is that you're using an IE
specific method of instancing, rather than a more generic one such as:

// returns an XMLHttpRequest object or null
function getxhr() {
var xmlhttp = null;
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e1)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
xmlhttp = false;
}
}
if(!xmlhttp && (typeof(XMLHttpRequest) != 'undefined')) xmlhttp = new
XMLHttpRequest();
if (!xmlhttp) xmlhttp = null;
return xmlhttp;
}

Rgds

Denis McMahon
 
M

Mel Smith

Martin said:
Have you checked what statusText shows, what responseText shows?

Martin:

I checked the statusText yesterday. It was null. (with a status of 0)
There was no response text.

Here's the text that I sent from my CGI app to my Apache Server to send
out:

I've also tried text/plain, and have yet to try text/html
**********************************************
Content-Type: text/xml

228881|Mel Smith#Edmonton Riverside Golf Club# 1|*|Dec
??/10|03919|69.0/120|79|AI|9.4|~ 2| |Dec ??/10|08264|67.7/112|82|AI|14.4|~
3|*|Dec ??/10|03919|69.0/120|84|AI|14.1|~ 4| |Dec
??/10|03919|69.0/120|88|AI|17.9|~ 5|*|Dec ??/10|03919|69.0/120|83|AI|13.2|~
6|*|Nov ??/10|08264|67.7/112|81|AI|13.4|~ 7|*|Nov
??/10|00358|71.1/118|85|AI|13.3|~ 8|*|Nov ??/10|03919|71.2/125|85|AI|12.5|~
9|*|Nov ??/10|03919|69.0/120|80|AI|10.4|~10| |Oct
??/10|18580|69.7/121|87|AI|16.2|~11|*|Oct
??/10|18580|69.7/121|83|AI|12.4|~12| |Oct
??/10|18580|69.7/121|94|AI|22.7|~13|*|Oct
??/10|18580|69.7/121|82|AI|11.5|~14| |Oct
??/10|18580|69.7/121|88|AI|17.1|~15| |Sep
??/10|18611|68.9/117|85|I|15.5|~16| |Sep
??/10|18580|69.7/121|87|AI|16.2|~17|*|Sep
??/10|18611|68.9/117|81|I|11.7|~18| |Sep
??/10|18580|69.7/121|87|AI|16.2|~19| |Sep
??/10|18580|69.7/121|89|AI|18.0|~20| |Sep ??/10|18611|68.9/117|84|TI|14.6|
***************************************************

Chrome, Safari, and FF indicate that they have *received* the response
(i.e., the readyState == 4), but the status is 0 and not 200.

Does this mean that the response text is faulty ? If so, what part is
bad, and what should my content-type be ??

Thanks,

-Mel Smith
 
M

Mel Smith

Denis said:
where's the "HTTP = <something>" line?

Denis:

I've included the complete 'HTTP Post' section below (gleaned and used
from David Flanagan's book).

I tried to track thru the 'post' function and it 'seems' to work
correctly. That is, it sends the request to my server, my CGI Server
receives it, and formulates a response, and the browser 'receives' it and IE
accepts it perfectly. however, Chrome, FF and Safari reject it with a status
of 0 and responseText of null.

So, in my naivete, I'm guessing that the response or the response header
is faulty ??

I wish I could provide an url for this, and I treid the otyer day but
got an 'access denied' on my public urrl

Anyway, here's the complete HHTP javascript section:

**************************************
<script type="text/javascript">

// Make sure we haven't already been loaded
var HTTP;
if (HTTP && (typeof HTTP != "object" || HTTP.NAME))
throw new Error("Namespace 'HTTP' already exists");

// Create our namespace, and specify some meta-information
HTTP = {};
HTTP.NAME = "HTTP"; // The name of this namespace
HTTP.VERSION = 1.0; // The version of this namespace

// This is a list of XMLHttpRequest creation factory functions to try
HTTP._factories = [
function() { return new XMLHttpRequest(); },
function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
];

// When we find a factory that works, store it here
HTTP._factory = null;

/**
* Create and return a new XMLHttpRequest object.
*
* The first time we're called, try the list of factory functions until
* we find one that returns a nonnull value and does not throw an
* exception. Once we find a working factory, remember it for later use.
*/
HTTP.newRequest = function() {
if (HTTP._factory != null) return HTTP._factory();

for(var i = 0; i < HTTP._factories.length; i++) {
try {
var factory = HTTP._factories;
var request = factory();
if (request != null) {
HTTP._factory = factory;
return request;
}
}
catch(e) {
continue;
}
}

// If we get here, none of the factory candidates succeeded,
// so throw an exception now and for all future calls.
HTTP._factory = function() {
throw new Error("XMLHttpRequest not supported");
}
HTTP._factory(); // Throw an error
}

HTTP.post = function(url, values, callback) {
var request = HTTP.newRequest();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200 ) {
callback(request.responseText);
}
else {
callback(request.status);
}
}
}

request.open("POST", url);
// This header tells the server how to interpret the body of the request
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
// Encode the properties of the values object and send them as
// the body of the request.
request.send(HTTP.encodeFormData(values));
if (clbplr == "clb") clbrequest=request; // set global vrbl clbrequest
if (clbplr == "plr") plrrequest=request; // set global vrbl plrrequest
};

/**
* Encode the property name/value pairs of an object as if they were from
* an HTML form, using application/x-www-form-urlencoded format
*/
HTTP.encodeFormData = function(data) {
var pairs = [];
var regexp = /%20/g; // A regular expression to match an encoded space

for(var name in data) {
var value = data[name].toString();
// Create a name/value pair, but encode name and value first
// The global function encodeURIComponent does almost what we want,
// but it encodes spaces as %20 instead of as "+". We have to
// fix that with String.replace()
var pair = encodeURIComponent(name).replace(regexp,"+") + '=' +
encodeURIComponent(value).replace(regexp,"+");
pairs.push(pair);
}

// Concatenate all the name/value pairs, separating them with &
return pairs.join('&');
};

//************* end of HTTP section ***************
 
M

Mel Smith

Hi Denis & Martin:

I inserted some code in the function 'onReadyStateChange' to capture the
successive changes to the ready state as the request progresssed from 'open.
thru 'loaded'.

(1: Open, 2: Sent, 3. Receiving, 4: Loaded)

For IE7, the progressive Ready States were: 1 1 2 3 4 (why two 1
states ?? -- but status=200 worked perfectly)

For Chrome, the States were: 1 4 (i.e., no 2 or 3 status = 0)

For FF, the States were: 1124 (no 3 -- status = 0)

For Safari, the States were: 14 (no 2 or 3 -- status = 0)

I'm very puzzled here !

I hope you folks can help me understand this.

Thank you.

-Mel Smith
 
M

Mel Smith

Richard said:
The first thing to mention is that - status - property of XML HTTP request
objects is supposed to report the HTTP status of a response, and HTTP does
not define a status of zero. Any response from a (correctly working) HTTP
server (success or fail, of any kind) should have a an HTTP status in the
set defined by HTTP.

Seeing a status of zero may suggest that the code that sees the 'response'
is seeing it outside of an HTTP context. For example, on Windows machines
failures in the TCP/IP system (such as a failed/no network connection)
produce status in the 12,000 range (well outside of anything HTTP
defines). I have also seen reports associating statuses of zero with XML
HTTP requests being made to the local file system (so no HTTP involvement
there at all).

One detail that is absent from the code you have posted is the origin,
contents of and handling of, the URL you are using. Your mentioning of CGI
and HTTP servers suggests that it would be difficult for any browser to
interpret the request as a request to the local file system, but still,
that is something worth checking.

Personally, for this I would use an HTTP traffic monitoring proxy (such as
Charles <URL: http://www.charlesproxy.com/ > (Java-based so needs a
(fairly recent) Java runtime environment)) and verify that the expected
request was actually being made and to see if the response (if any)
included a proper HTTP status or really was zero. (if the request/response
did happen and the server response with a status of zero then the problem
is on the server and no amount of looking at client side scripts will fix
that).

There was a time when XML HTTP request objects were very sensitive to the
order in which they were set up. That is, you had to do - open -, and then
setting up (like setting headers), then attach the - onreadystatechange -
listener, and then do the - send - call. The code you are using attaches
the - onreadystatechange - listener before it calls - open -. My
impression was that this issue had been fixed and the order of attaching
the - onreadystatechange - listener was no longer significant (so you
would not expect an 'across the board' failure on a whole set of browsers
if this were your issue).

However, I am not certain about this as I am still using XML HTTP request
handling module(s) that I wrote back in 2003, and they sequence things
such that they avoid the original issue. If nothing else stands out, it
would be pretty easy/cheep to bump the attachment of the -
onreadystatechange - listener down in the code to just before the - send -
call, which, even if it does not cure the issue, would at least rule out
that possibility.


Richard:

I'm going to print out your response and study it over the weekend. Its
a bit 'deep' to me but I'll dig in and see what I can do. I'll reply when
I've got something intelligible to say.

Thanks you for the detailed and thorough response.

-Mel
 
M

Mel Smith

Richard:

Here is my 'post' call from the client browser

******************
var plrobj =
{formname:document.getElementById("iformname").value,playerid:eek:bj.value}
// Now tell the 'post' function who's calling with a semaphore
clbplr = "plr" // will be looked at in the .post function
HTTP.post("http://ww2.frostdelay.com:4296",plrobj,getplrname);
***************************


Also, I don't quite understand how IE can run perfectly when the others
don't ??

Thanks again.

Mel Smith
 
M

Mel Smith

I said:
I inserted some code in the function 'onReadyStateChange' to capture
the successive changes to the ready state as the request progresssed from
'open. thru 'loaded'.

(1: Open, 2: Sent, 3. Receiving, 4: Loaded)

For IE7, the progressive Ready States were: 1 1 2 3 4 (why two 1
states ?? -- but status=200 worked perfectly)

For Chrome, the States were: 1 4 (i.e., no 2 or 3 status = 0)

For FF, the States were: 1124 (no 3 -- status = 0)

For Safari, the States were: 14 (no 2 or 3 -- status = 0)

Richard:

Following your testing suggestion, I moved the 'onReadyStateChange'
function dow to just before the 'Send()' functions.

Here are the State changes I recorded:

IE7: States: 1 2 3 4 perfect with status = 200
Chrome: States: 4 status = 0
Safari: States: 4 status = 0
FF: States: 4 status == 0

I'm going back in and modify my recording of the states to ensure I
really got a numeric status of zero -- maybe it was null. I'll let you know
shortly

( a few minutes later)
No, The typeof status is *undefined* which I incorrectly transformed
into 0

Thanks

-Mel Smith
 
M

Mel Smith

Richard said:

Richard Cornford said:
Now we come the question of whether this is a cross-domain request or not.

Richard:

For testing purposes, I created a test page on my Development machine,
then at the command prompt, I enter the command:

C:\>explorer mytestpage.htm // I actually have a batch file that
shortens this: C:\>SHOW mytestpage

// also, I have similar batch files for quick testing of the other
browsers I use
// for example C:\>SHOSAF mytestpage // uses Safari for testing

In this fashion, I don't have to pass the page to my server because it
already exists on my dev machine. I just 'run' it -- so to speak

Since *this* testpage is running on my development machine, and since
the Apache Server is on a *different machine* beside me,

Does this constitute a cross-domain situation ??

If it does, then what I have to do (even for testing) is to place my
test page as an *actual page* to be served on the server machine itself.


I hope I understand you correctly about this 'cross-domain' situation.

btw, the url in myjavascript code in a previous post I use is the actual
address to access my cgi app (which is a c-based executable). I by-passed
the name conversion done by zoneedit to my *actual* address

Thank you !

-Mel Smith
 
M

Mel Smith

Hi Richard:

It *was* a cross-domain problem !!

I placed the testpage on my server, did a few changes in the cgi app,
and it works on Chrome -- and I assume FF, and Safari too !!

Thank you for the help !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top