Welcome to WebmasterWorld Guest from 54.196.175.173

Forum Moderators: open

Message Too Old, No Replies

AJAX event not firing in Safari

XMLHttpRequest is created by url not being sent

     

pillsbur

4:54 pm on Jun 29, 2007 (gmt 0)

5+ Year Member



Hi all,

The subtitle should read "XMLHttpRequest is created BUT url not being sent". Sorry.

I'd appreciate any help with a problem I'm having with Safari 2.0.4 and ajax. My code works fine with FF and Camino (Mac browser), as well as IE 7.

It seems the

open
and
send
methods are not working -- my XMLHttpRequest is being created but nothing is actually sent. I've tested for readystate (using alert()) and it's always 0 in Safari (it's 4 in FF, which it should be).

Here's what Safari outputs to the browser when the ajax code is run:
readyState: 0
server status: undefined
status text: undefined

Here's FF's output:
readyState: 4
server status: 200
status text: OK

Here's the relevant code; I'd appreciate any insight. I know Safari can do ajax just fine, so I'm stumped as to why it's not working here.

// Create the url and incorporate a passed variable "sysno"
var x_statement = "http://aleph1.pacific.edu/X?OP=CIRC-STATUS&SYS_NO=" + sysno + "&LIBRARY=pac01";
xmlHttp = GetXmlHttpObject(); // another function creates the object; alert() tells me it's been created
xmlHttp.open("GET",x_statement,false); // For technical reasons I need to do false, however true doesn't change anything
xmlHttp.send(null); // And here we send

if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)) { // I don't actually need to test for readyState because I'm using false, but what the heck...
xmlDoc = xmlHttp.responseXML;

.... and so on ....

Finally, this is my code for creating the HttpRequestObject. As I mentioned, the object *is* created, but maybe the code will be helpful still:

function GetXmlHttpObject() {
var objXMLHttp = null;
if (window.XMLHttpRequest) {
objXMLHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert("Browser too old. Please upgrade");
}
return objXMLHttp;
} // End of function

Ideas? Have you run into something like this?

Thanks,
Glenn

StupidScript

7:19 pm on Jun 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's tough to tell why Safari would have problems using a Microsoft invention (XMLHttpRequest)(!), but here's what I use without any reported issues:

var msxmlhttp = new Array(
'Msxml2.XMLHTTP.5.0',
'Msxml2.XMLHTTP.4.0',
'Msxml2.XMLHTTP.3.0',
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP');
for (i=0;i<msxmlhttp.length;i++) {
try {
thisConn = new ActiveXObject(msxmlhttp[i]);
} catch (e) {
thisConn = null;
}
}
if(!thisConn && typeof XMLHttpRequest!= "undefined") {
thisConn = new XMLHttpRequest();
}
if (!thisConn) {
thisConn = false;
}

You may want to add a couple of alerts in the chain to see if you can get the actual

typeof
value Safari understands, and add that to the options.

There's a few notes about Safari bugs with XMLHttpRequest from a couple of years ago, here [bitterpill.org]:

The XMLHTTPRequest object in javascript with safari does, on occasion, set the status for the return request as 'undefined'. It seems this is a problem with safari 1.3, and I do believe in 2.0 as well, when trying to retrieve pages from the browser cache using XMLHTTPRequest or AJAX as the kids are calling it these days.

pillsbur

10:35 pm on Jun 29, 2007 (gmt 0)

5+ Year Member



Thanks for the reply, but I'm still stuck. Here's my latest ajax call:

var xstatement = "http://aleph1.pacific.edu/X?OP=CIRC-STATUS&SYS_NO=" + sysno + "&LIBRARY=pac01";
xmlHttp = GetXmlHttpObject();
alert("Line 113 "+xmlHttp);
xmlHttp.open("GET",xstatement,false);
alert("Line 115 "+xmlHttp);
xmlHttp.setRequestHeader('If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT');
xmlHttp.send(null);
alert (xmlHttp.getRequestHeaders);
alert (xmlHttp.readyState);

The alerts for line 113 and 115 appear and confirm the xmlHttp is an object (like it's supposed to be).

The setRequestHeader method seems to have no effect, perhaps because my process doesn't actually retrieve a physical XML document (hence there's no date to compare against 1995?) -- it's a just a server call that gets returned as XML, but that's not too unusual, right?

Finally, xmlHttp.getRequestHeaders returns as

undefined
(there's lots of stuff when it's called from FF) and xml.readyState is still 0.

Thanks again, and if you have anything else, I'm all ears.

Glenn

jatar_k

2:50 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



bump

pillsbur

3:42 pm on Aug 2, 2007 (gmt 0)

5+ Year Member



Here's the solution: In our case Safari required the URL to be relative, not absolute. Once we chopped off the server name (and kept the first "/") it started to work perfectly. FF and IE aren't affected by the change, so all is good. No idea why Safari seemed to fail with an absolute URL and I've not seen any documentation about it, so YMMV.

Glenn

 

Featured Threads

Hot Threads This Week

Hot Threads This Month