2012-03-03 182 views
0

我試圖使這個:純JS阿賈克斯jQuery的阿賈克斯

if (window.XMLHttpRequest) { 
     xmlhttp = new XMLHttpRequest(); 
    } 
    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
     document.getElementById("txtuser").innerHTML = xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET", "ajax.php?operation=get" + "&q=" + str, true); 
    xmlhttp.send(); 

jQuery的...

這是我有:

$.ajax({ 
      type: "GET", 
      url: "ajax.php", 
      data: "operation=get" + "&q="+ str, 
      success: function(){ 
       console.log('done'); 
      } 
     }); 
    return false; 

就是我我做錯了?

+1

ajax.php是什麼樣的?你在控制檯中發現錯誤嗎?你確定對ajax.php的請求是成功的嗎? – ngen 2012-03-03 19:13:07

+0

控制檯可以讓您查看請求,而不需要請求本身的反饋,這可能是一些不同的問題 – charlietfl 2012-03-03 19:54:05

回答

0

也許你是通過你的數據在錯誤的道路..嘗試與

data: { operation: 'get', q: str } 
3

我覺得你的問題是與您的數據。 jQuery是期待中的數據是JSON,所以你需要的是更多這樣的:

$.ajax({ 
     type: "GET", 
     url: "ajax.php", 
     data: { operation: 'get', q: str}, 
     success: function(){ 
      console.log('done'); 
     } 
}); 
return false; 

應該做你想做的,jQuery將自動對其進行編碼,進入網址爲您的請求時。