XMLHttpRequest()将无法在Safari中打开 [英] XMLHttpRequest() won't open in Safari

查看:117
本文介绍了XMLHttpRequest()将无法在Safari中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Javascript编程的新手,我在Safari浏览器中创建了一个XMLHttpRequest,我遇到了一些麻烦。实际上,

请求在Firefox中也不起作用,但仅当我使用Mac

计算机时。基于Windows的系统上的IE和FireFox工作得很好。我遇到的

问题是XMLHttpRequest没有打开我编入其中的网站

。当我检查readyState时,它只有

返回0并且永远不会通过其他。我的代码如下:


var xml_request = null;


// Mozilla

if(window。 XMLHttpRequest)

{

xml_request = new XMLHttpRequest();


/ *试试{


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);

}


再一次,它适用于基于Windows的计算机。但是Mac上的Firefox

和Safari浏览器是我遇到麻烦的地方。

解决方案

2006-10-05 22:24:24 +0200, TL ********* @ gmail .com 说:


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);



这会导致每个浏览器出错。

事件处理程序必须是一个函数。什么都不返回。


而不是你应该写:

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


xml_request.setRequestHeader(" Cache-Control"," no-cache");

xml_request.send(null);

}

再次,它适用于基于Windows的计算机。但是Mac上的Firefox

和Safari浏览器是我遇到麻烦的地方。



尝试使用工作处理函数。如果它仍然无法正常工作,

告诉我们你在两种浏览器中都会遇到什么错误。

-

David Junger


Touffy写道:


2006-10-05 22:24:24 +0200,< a href =mailto:TL ********* @ gmail.com> TL ********* @ gmail.com 说:


> var xml_request = null;

// Mozilla
if(window.XMLHttpRequest)



和IE7,以及Opera和其他人。


> {
xml_request = new XMLHttpRequest();

xml_request.open(" GET",feed,false);



为什么在这里包含请求?获取对XMLHttpRequest

对象的引用,如果成功,则执行请求。


var httpRequest = getRequestObject();

if(httpRequest){

/ * ... * /

}


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



这会导致每个浏览器出错。



不一定:它取决于在调用send方法之前它们如何处理readyState

属性的访问权限。


这方面真的不应该有问题。该属性是

描述为在调用send方法之前调用open方法

之前返回0(未初始化)和1(加载)。
< blockquote class =post_quotes>
事件处理程序必须是一个函数。



null和undefined值应理解为no listener。


警报方法没有回报。



返回undefined,这不是一回事。 :-)


Mike

如果XMLHttpRequest和

createRequest的类型(对象与函数)一致,则条件下面可以简化。我知道,对于前者,它最有可能是''功能'',但是MSIE

习惯使用''对象'为某些主机我可能从未使用过IceBrowser(使用createRequest)。


函数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)

试试{

object = new ActiveXObject(''Msxml2.XMLHTTP'');

} catch(e){

try {

object = new ActiveXObject(''Microsoft.XM LHTTP'');

} catch(e){

object = null;

}

}

@end @ * /

}

返回对象;

}


另一个实现可以执行一次测试,用一个更简单的替换

getRequestObject函数。


Michael Winter说:


Touffy写道:


>这会导致每个浏览器出错。
一个事件handler必须是一个函数。



null和undefined值应理解为no listener。



当然没有错误。我想让我睡一觉的时间。


>警报方法什么都不返回。



返回undefined,这不是一回事。 :-)



是的,是的。 undefined并不是什么都没有,它既是类型又是价值

所有的东西都不再存在,或者不存在,或者永远不会存在。并且

有些东西。这是过去,未来,以及所有未来发生的JavaScript潜力。所以我猜它比所引用的所有已定义JavaScript值的总和还要大。

。那是

相当的东西:)

-

David Junger


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.

解决方案

On 2006-10-05 22:24:24 +0200, TL*********@gmail.com 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


Touffy wrote:

On 2006-10-05 22:24:24 +0200, TL*********@gmail.com 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.


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


这篇关于XMLHttpRequest()将无法在Safari中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆