欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Jquery在IE7下無法使用 $.ajax解決方法

 更新時間:2009年11月11日 17:45:32   作者:  
今天在做系統(tǒng)測試的時候,原本用Jquery寫了一個動態(tài)加載的樹形菜單,發(fā)現(xiàn)在IE7下無法加載數(shù)據(jù),(采用的是jquery1.3.2版本的$.ajax方法),上網(wǎng)查詢到原來是IE7的執(zhí)行ajax是用XMLHTTPRequest來聲明的,經(jīng)過對比果然如此;后采用以下的方法隨即解決了問題。
通過查看源碼發(fā)現(xiàn)
復制代碼 代碼如下:

// 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 ”的結(jié)果可以知道IE6、IE7和IE8都是返回的true,
測試的html源碼為(同一個目錄下有一個名為index.jsp頁面,內(nèi)容無所謂。)
復制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="javascript/jquery.js"></script>
<script type="text/javascript">
$(function (){
$.ajax({
url:"index.jsp",
success:function(){alert("success")},
error:function(){alert("error")}
});
//$("div").append("<font color='red'>window.ActiveXObject:</font>");
//$("div").append((window.ActiveXObject?"true":"false"));
//alert(typeof(new XMLHttpRequest()));
//alert(typeof(new ActiveXObject("Msxml2.XMLHTTP.4.0")));
//alert(typeof(new ActiveXObject("Msxml2.XMLHTTP")));
//alert(typeof(new ActiveXObject("Microsoft.XMLHTTP")));
});
</script>
</head>
<body>
<div></div>
</body>
</html>

情況一:
不修改源碼,則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的初始化方法進行兼容,而官網(wǎng)上的兼容說明是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()創(chuàng)建,那就是說,
如果我不修改的話IE7應該也是可以的。
注:最后這個多一句的IE7兼容辦法是在網(wǎng)絡上搜索到的
以上就是我今天研究的結(jié)果,弄的我很糊涂,到底這個IE6、IE7和IE8全面兼容的jQuery的到底怎么實現(xiàn)(不能影響FireFox等等哦)
如果結(jié)論是:
由此可見IE7中需要使用new XMLHttpRequest()初始化ajax對象,IE6則使用new ActiveXObject("Microsoft.XMLHTTP")
那么prototype中的又怎么解釋?
我已經(jīng)糊涂了,希望大家指點一二!
剛才又搜索了一下關(guān)于XMLHttpRequest的創(chuàng)建方法,最后將源碼修改為
return window.XMLHttpRequest? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
則IE6\7\8和FF都可以運行了。
總算是解決了。

相關(guān)文章

最新評論