Maison > interface Web > js tutoriel > le corps du texte

Jquery在IE7下无法使用 $.ajax解决方法_jquery

WBOY
Libérer: 2016-05-16 18:42:08
original
1422 Les gens l'ont consulté

通过查看源码发现

复制代码 代码如下:

// Create the request object; Microsoft failed to properly
// implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
// This function can be overriden by calling jQuery.ajaxSetup
xhr:function(){
return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
},

以下是这个jquery的源码的版本声明
复制代码 代码如下:

/*
* jQuery JavaScript Library v1.3.2
* http://jquery.com/
*
* Copyright (c) 2009 John Resig
* Dual licensed under the MIT and GPL licenses.
* http://docs.jquery.com/License
*
* Date: 2009-02-19 17:34:21 -0500 (Thu, 19 Feb 2009)
* Revision: 6246
*/

通过一个html打印“window.ActiveXObject ”的结果可以知道IE6、IE7和IE8都是返回的true,
测试的html源码为(同一个目录下有一个名为index.jsp页面,内容无所谓。)
复制代码 代码如下:





Insert title here








情况一:
不修改源码,则IE6中可以弹出“success”的提示,而IE7却没有任何提示,连错误提示都没有。
情况二:
将源码中的
window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
修改为
window.ActiveXObject ? new XMLHttpRequest() : new XMLHttpRequest();
则IE7中是弹出“success”的提示,而IE6却提示js错误,详情大概为”XMLHttpRequest对象未定义“
两种情况下FireFox都可以正常提示“success”,版本是FireFox3.5.3,其他浏览器不知道。
由此可见IE7中需要使用new XMLHttpRequest()初始化ajax对象,IE6则使用new ActiveXObject("Microsoft.XMLHTTP")
但是jQuery源码中却没有对IE7的初始化方法进行兼容,而官网上的兼容说明是IE6+。
难道是我理解错误,还是其他的什么?希望大家给点意见,jQuery很好用,但是我不能要求客户必须用IE6,而放弃IE7啊!
最后多一句:
prototype最新版1.6.1也是同样的问题IE7下的Ajax.Request是没有作用的,需要将源码的1130行左右的
复制代码 代码如下:

var Ajax = {
getTransport: function() {
return Try.these(
function() {return new XMLHttpRequest()},
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
//function() {return new ActiveXObject('Msxml2.XMLHTTP.4.0')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')}
) || false;
},

进行修改,注释的那个部分是需要添加的修改。
这样才可以在IE7下使用这个ajax请求方法。
可是大家粗略的看一下,这里面的ajax初始化是先使用new XMLHttpRequest()创建,那就是说,
如果我不修改的话IE7应该也是可以的。
注:最后这个多一句的IE7兼容办法是在网络上搜索到的
以上就是我今天研究的结果,弄的我很糊涂,到底这个IE6、IE7和IE8全面兼容的jQuery的到底怎么实现(不能影响FireFox等等哦)
如果结论是:
由此可见IE7中需要使用new XMLHttpRequest()初始化ajax对象,IE6则使用new ActiveXObject("Microsoft.XMLHTTP")
那么prototype中的又怎么解释?
我已经糊涂了,希望大家指点一二!
刚才又搜索了一下关于XMLHttpRequest的创建方法,最后将源码修改为
return window.XMLHttpRequest? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
则IE6\7\8和FF都可以运行了。
总算是解决了。
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!