2010-01-21 140 views
2

我有一個腳本運行在xampp上,當我運行此警報調用腳本時,該文件執行,但是當我運行它沒有警報時,它只是坐在那裏,並沒有返回任何東西。我究竟做錯了什麼?Ajax腳本有時只返回結果?

代碼:

var xhr;

function getPlants(xhr){ 嘗試xhr = new XMLHttpRequest(); (微軟){
} } (其他微軟){
} catch(othermicrosoft){ 嘗試xhr = new ActiveXObject(「Microsoft.XMLHTTP」);
} catch(失敗){0} xhr = false; alert(「ajax not supported」); }}
}

xhr.onreadystatechange=state_change;  
xhr.open("GET","db_interactions.php",true);  
xhr.send(null); 
    alert("sent"); 


function state_change() { 
    if(xhr.readyState==4 && xhr.status==200) { 
     alert("all systems go"); 
     return xhr.responseText; 
    } 

}

+0

@Daquan - 如果你刪除並替換警報一個接一個,哪一個會停止工作? – 2010-01-21 20:23:06

回答

0

我會強烈建議使用jQueryPrototype。他們是JavaScript庫,在調用Ajax調用以及許多其他JavaScript操作時將使您的生活更輕鬆。

阿賈克斯例如用jQuery:

$.ajax({ 
    type: "POST", 
    url: "some.php", 
    data: "name=John&location=Boston", 
    success: function(msg){ 
    alert("Data Saved: " + msg); 
    } 
});