2014-04-29 107 views
0

以下(縮寫)代碼與輸入框中的自動完成類似,將結果輸出到輸入框下方的div中。它可以在Chrome/Firefox中完美地工作(即搜索「Eggs」,然後「Milk」顯示他們的結果),但是在IE中它使第一個請求很好,但是它不會輸出任何進一步的請求。多個請求在IE中未呈現的AJAX輸出

網絡顯示IE正在向服務器發送後續請求(200頭代碼),但是它使用它做任何事情。

編輯:我不能使用jQuery。但可以使用YUI3(平臺限制)

/* Setup Ajax */ 
function ajaxRequest(){var activexmodes=["Msxml2.XMLHTTP", Microsoft.XMLHTTP"],i;if(window.ActiveXObject){for(i=0; i<activexmodes.length; i++){try{return new ActiveXObject(activexmodes[i]);}catch(ignore){/*suppress error*/}}}else if (window.XMLHttpRequest){return new XMLHttpRequest();}else{return false;}} 

/* Get Value of search */ 
var searchBoxObj = document.getElementById('searchBox-1'); 

/* The Ajax Request */ 
var theSearchValue; 
var mygetrequest=new ajaxRequest(); 
mygetrequest.onreadystatechange=function(){ 
if (mygetrequest.readyState==4){ 
    if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){ 
    var jsondata=JSON.parse(mygetrequest.responseText), /* retrieve result as an JavaScript object */ 
     searchData = jsondata.searches, 
     i; 
    if (searchData.length > 0){ 
     document.getElementById('result').className = "on"; 
     output='<ul id="searchResults">'; 
     for (i=0; i < searchData.length; i++){ 
     /* The Loop Code */ 
    } 
    output+='</ul>'; 
    } 
    else { 
    document.getElementById('result').className = ""; 
    } 

    document.getElementById("result").innerHTML = output; 
    } 
    else{ 
    /* alert("An error has occured making the request") */ 
    } 
} 
}; 

/* With every key press */ 
var stoppedTyping; 
searchBoxObj.onkeyup = function() { 

    if (stoppedTyping) { clearTimeout(stoppedTyping);} 
    stoppedTyping = setTimeout(function(){ 
     if (searchBoxObj.value.length > 2){ 
     theSearchValue = searchBoxObj.value; 
     mygetrequest.open("GET", "/asp/lookup.asp?term="+theSearchValue, true); 
     mygetrequest.send(null); 
     } 
    }, 200); 
}; 

回答

0

也許你可以嘗試使用jQuery來處理Ajax請求,你的結果div的填充。 因爲它被設計爲跨瀏覽器,應該正確支持IE瀏覽器(版本2. *的jquery將只支持8版本,而1.9版本*將支持IE版本6) 該代碼可能看起來像這樣的東西:

$.ajax({ 
    type: "GET", 
    url: your_url 

}) 
    .done(function(data) { 
    var jsondata=JSON.parse(data); 
    var searchData = jsondata.searches; 
    if (searchData.length > 0){ 
     $('#result').attr("class","on"); //adding the class attr 
     output='<ul id="searchResults">'; 
     for (var i=0; i < searchData.length; i++){ 
     /* The Loop Code */ 
    } 
    output+='</ul>'; 
    } 
    else { 
    $('#result').attr("class",""); 
    } 

$("#result").html(output); //filling the dom with your output string 

}) 
.fail(function() { 
    alert("error"); 
    }) 

看到jQuery的文檔:https://api.jquery.com/jQuery.ajax/

+0

感謝您的回答,但是我不能在該平臺上使用jQuery。我可以使用YUI3然而, – acasperw

+0

嘿好吧我的壞,我認爲這可能是一個問題,在IE中的事件處理可能已被糾正使用jQuery;) – mazuno