Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > XMLHttpRequest() won't open in Safari

Reply
Thread Tools

XMLHttpRequest() won't open in Safari

 
 
TLaufenberg@gmail.com
Guest
Posts: n/a
 
      10-05-2006
I'm new to Javascript programming and I've run into a bit of a snag
with making an XMLHttpRequest in the Safari browser. Actually, the
request doesn't work in Firefox either but only when I use a Mac
computer. IE and FireFox on a Windows based system works just fine. The
problem I am having is that the XMLHttpRequest doesn't open the site
that I've programmed into it. When I check the readyState it only
returns 0 and never goes through the others. My code is as follows:

var xml_request = null;

//Mozilla
if(window.XMLHttpRequest)
{
xml_request = new XMLHttpRequest();

/* try {

netscape.security.PrivilegeManager.enablePrivilege ("UniversalBrowserRead");
} catch (e) {
}*/

xml_request.open("GET", feed, false);
xml_request.onreadystatechange = alert(xml_request.readyState);
xml_request.setRequestHeader("Cache-Control", "no-cache");
xml_request.send(null);
}
else if(window.ActiveXObject)
{
//IE
xml_request = new ActiveXObject("Microsoft.XMLHTTP");

xml_request.open("GET", feed, false);
xml_request.setRequestHeader("Cache-Control", "no-cache");
xml_request.send(null);
}

Again, it works on Windows based computer just fine. But the Firefox
and Safari browser on a Mac are where I'm having trouble.

 
Reply With Quote
 
 
 
 
Touffy
Guest
Posts: n/a
 
      10-05-2006
On 2006-10-05 22:24:24 +0200, http://www.velocityreviews.com/forums/(E-Mail Removed) said:

> var xml_request = null;
>
> //Mozilla
> if(window.XMLHttpRequest)
> {
> xml_request = new XMLHttpRequest();
>
> xml_request.open("GET", feed, false);
> xml_request.onreadystatechange = alert(xml_request.readyState);


This should cause an error in every browser.
An event handler must be a function. The alert method returns nothing.

instead you should write:
xml_request.onreadystatechange = function(){alert(xml_request.readyState)}

> xml_request.setRequestHeader("Cache-Control", "no-cache");
> xml_request.send(null);
> }
> Again, it works on Windows based computer just fine. But the Firefox
> and Safari browser on a Mac are where I'm having trouble.


Try it with the working handler function. If it still doesn't work,
tell us what errors you get in both browsers.


--
David Junger

 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      10-05-2006
Touffy wrote:

> On 2006-10-05 22:24:24 +0200, (E-Mail Removed) said:
>
>> var xml_request = null;
>>
>> //Mozilla
>> if(window.XMLHttpRequest)


And IE7, and Opera, and others.

>> {
>> xml_request = new XMLHttpRequest();
>>
>> xml_request.open("GET", feed, false);


Why include the request here? Obtain a reference to a XMLHttpRequest
object and, if successful, execute the request.

var httpRequest = getRequestObject();

if (httpRequest) {
/* ... */
}

>> xml_request.onreadystatechange = alert(xml_request.readyState);

>
> This should cause an error in every browser.


Not necessarily: it depends how they handle access of the readyState
property before the send method is called.

There shouldn't really be a problem in this regard. The property is
described as returning 0 (uninitialised) before the open method is
called, and 1 (loading) before the send method is called.

> An event handler must be a function.


The null and undefined values should be understood as "no listener".

> The alert method returns nothing.


It returns undefined, which isn't quite the same thing.

Mike


If the types (object versus function) of both XMLHttpRequest and
createRequest are consistent, the conditions below can be simplified. I
know that for the former, it is most likely to be 'function', but MSIE
has a habit of using 'object' for some host functions which might extend
to IE 7. I've never used IceBrowser (which uses createRequest).

function getRequestObject() {
var object = null;

if ((typeof XMLHttpRequest == 'object')
|| (typeof XMLHttpRequest == 'function'))
object = new XMLHttpRequest();
else if ((typeof createRequest == 'object')
|| (typeof createRequest == 'function'))
object = createRequest();
else if (typeof ActiveXObject == 'function') {
/*@cc_on @*/
/*@if(@_jscript_version >= 5)
try {
object = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
try {
object = new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {
object = null;
}
}
@end @*/
}
return object;
}

An alternate implementation could perform the tests once, replacing the
getRequestObject function with a simpler one.
 
Reply With Quote
 
Touffy
Guest
Posts: n/a
 
      10-06-2006
Michael Winter said:

> Touffy wrote:
>> This should cause an error in every browser.
>> An event handler must be a function.

>
> The null and undefined values should be understood as "no listener".


no error indeed, of course. Time for me to have some sleep I guess.

>> The alert method returns nothing.

>
> It returns undefined, which isn't quite the same thing.


Yeah, true. undefined is not nothing, it is both the type and the value
of everything that is no more, has yet to be, or will never be. And
some things that are. It is the past, the future, and all the
never-to-happen potential of JavaScript. So I guess it's bigger than
the sum of all the defined JavaScript values ever referenced. That's
quite something


--
David Junger

 
Reply With Quote
 
TLaufenberg@gmail.com
Guest
Posts: n/a
 
      10-06-2006
>
> > var xml_request = null;
> >
> > //Mozilla
> > if(window.XMLHttpRequest)
> > {
> > xml_request = new XMLHttpRequest();
> >
> > xml_request.open("GET", feed, false);
> > xml_request.onreadystatechange = alert(xml_request.readyState);

>
> This should cause an error in every browser.
> An event handler must be a function. The alert method returns nothing.
>
> instead you should write:
> xml_request.onreadystatechange = function(){alert(xml_request.readyState)}
>
> > xml_request.setRequestHeader("Cache-Control", "no-cache");
> > xml_request.send(null);
> > }


Touffy,

The suggestion you made is exactly what I have only I didn't create a
function out of it. No need in writing more code then I really have to.
Like I said, my entire program works just fine on a Windows based
system. I even got the program to work in FireFox on the Mac, I forgot
to set the browser property 'signed.applets.codebase_principal_support'
to true but I still can't get it to work in Safari. I'm beginning to
think that Safari has a similar safeguard in place that I need to turn
off somehow in order to allow my program to make a call to the method
XMLHttpRequest.open b/c I believe that the method is being blocked
instead of having an error in my code.

 
Reply With Quote
 
Touffy
Guest
Posts: n/a
 
      10-06-2006
(E-Mail Removed) said:

> Touffy,
>
> The suggestion you made is exactly what I have only I didn't create a
> function out of it. No need in writing more code then I really have to.


When you write:

>> xml_request.onreadystatechange = alert(xml_request.readyState);


it does this:

alert(xml_request.readyState);
xml_request_onreadystatechange=undefined;

is it what you want to do ?

> I'm beginning to
> think that Safari has a similar safeguard in place that I need to turn
> off somehow in order to allow my program to make a call to the method
> XMLHttpRequest.open b/c I believe that the method is being blocked
> instead of having an error in my code


I'm currently writing a web application using XMLHttpRequest. I'm
working on a Mac, so I'm testing on Safari and FF (IE's turn will come
when everything works in "real" browsers ), and both browsers
behave exactly as I expected. The code I wrote doesn't have any tricky
privilege setting. I tested first with synchronous, then asynchronous
mode, made the code progressively more complex, and it always worked on
the first try (it was my first time using XMLHttpRequest).
XMLHttpRequest is supposed to need special privileges only if your page
requests a URL outside its domain (so if you don't write "http://" in
the request URL, it should work fine without any privilege setting).

So, when you test on Safari, does it throw an error (you have Safari's
JavaScript console open, don't you ?) ? Does FF throw an error when you
test without being privileged ?
If they do, please tell us. I can't see it in the code you posted, it
may be somewhere else.

--
David Junger

 
Reply With Quote
 
TLaufenberg@gmail.com
Guest
Posts: n/a
 
      10-06-2006
Touffy,

I should have said it before but I'm trying to perform a cross-domain
XMLHttpRequest and that is where my problem is rooted. We're trying to
add RSS feeds to our Intranet.

xml_request.onreadystatechange = alert(xml_request.readyState);

The above line of code works exactly the same way as your line of code:

xml_request.onreadystatechange =
function(){alert(xml_request.readyState)}

All that is being done here is when the readyState changes, to do
something. So if it is set equal to a function or just the alert itself
doesn't matter. I've realized what the problem is and it is that
cross-domain XMLHttpRequests are not friendly. I found the workaround
for FireFox which is:

try {
user_pref("signed.applets.codebase_principal_suppo rt", true);

netscape.security.PrivilegeManager.enablePrivilege ("UniversalBrowserRead");
} catch (e) {
}

But I'm unsure of how to do this in Safari and Internet Explorer. If I
wasn't dealing with the cross-domain non of this would be an issue. If
you have any suggestions on how I get Safari and Internet Explorer to
allow this request that would be greatly appreciated.


-Tracy

 
Reply With Quote
 
TLaufenberg@gmail.com
Guest
Posts: n/a
 
      10-06-2006
Slight correction in the previous posting:

try {

netscape.security.PrivilegeManager.enablePrivilege ("UniversalBrowserRead");
user_pref("signed.applets.codebase_principal_suppo rt", true);
} catch (e) {
}

I had the netscape and user_pref backwards in the other posting.


-Tracy

 
Reply With Quote
 
Touffy
Guest
Posts: n/a
 
      10-07-2006
(E-Mail Removed) said:

> I should have said it before but I'm trying to perform a cross-domain
> XMLHttpRequest and that is where my problem is rooted. We're trying to
> add RSS feeds to our Intranet.


I see. Maybe you could use some server-side script (or just a cron'd
wget or curl or some Windows equivalent) to retrieve the RSS file and
store it locally so it can be accessed by your script without the
privilege problem ?

> xml_request.onreadystatechange = alert(xml_request.readyState);
>
> The above line of code works exactly the same way as your line of code:
>
> xml_request.onreadystatechange =
> function(){alert(xml_request.readyState)}


did you test that ?

> All that is being done here is when the readyState changes, to do
> something.


actually your line ensures nothing is done by setting the
onreadystatechange handler to undefined, which is the return value of
the window.alert method.

> I've realized what the problem is and it is that
> cross-domain XMLHttpRequests are not friendly. I found the workaround
> for FireFox which is:
>
> try {
> netscape.security.PrivilegeManager.enablePrivilege ("UniversalBrowserRead");
> } catch (e) {
> }
>
> But I'm unsure of how to do this in Safari and Internet Explorer.


I know IE uses a different scheme for script privileges. There are
several sets of websites defined in IE's preferences, with privileges
associated with each set.

On Safari, I'm not sure this can even be done.

> If I wasn't dealing with the cross-domain non of this would be an issue.


(see the top of this post)


--
David Junger

 
Reply With Quote
 
TLaufenberg@gmail.com
Guest
Posts: n/a
 
      10-09-2006
Touffy,

> > xml_request.onreadystatechange = alert(xml_request.readyState);


This line of code doesn't return nothing or even undefined. It returns
a value of 0 and never progresses beyond that. I don't know how to do
it but I have to set Safari to allow access to data sources across
domains.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: webbrowser.open always opens up Safari on Lion Steven D'Aprano Python 3 02-26-2012 07:12 AM
Re: how to open a file in some application using Tkinter i am usingTKINTER to create GUI application i want to know how to open a worddocument in open office or any other applicatio Fredrik Lundh Python 1 01-09-2008 10:40 AM
Why won't window.onload fire after window.open? - or - Safari and IE, how I loathe thee cgrady@gmail.com Javascript 2 08-11-2006 12:05 AM
Safari Browser compatible with dotnet Hemlata Shah ASP .Net 2 03-04-2004 04:39 AM
Safari Display Problem Jane Hawkins ASP .Net 1 12-23-2003 03:09 PM



Advertisments