2012-12-06 86 views
-2

我正在做類似這樣的事情,我通過AJAX發佈一個變量給php腳本。它實際上進入我的PHP和運行我的回聲,但它不打印任何東西,當我打印$ _POST數組。通過AJAX POST傳遞信息

function ajaxFunction(data){ 
var ajaxRequest; // The variable that makes Ajax possible! 

try{ 
    // Opera 8.0+, Firefox, Safari 
    ajaxRequest = new XMLHttpRequest(); 
} catch (e){ 
    // Internet Explorer Browsers 
    try{ 
     ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
    } catch (e) { 
     try{ 
      ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (e){ 
      // Something went wrong 
      alert("Your browser broke!"); 
      return false; 
     } 
    } 
} 

ajaxRequest.open("POST", "process.php", true); 
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
ajaxRequest.setRequestHeader("Content-length", data.length); 
ajaxRequest.setRequestHeader("Connection", "close"); 
// Create a function that will receive data sent from the server 
ajaxRequest.onreadystatechange = function(){ 
    if(ajaxRequest.readyState == 4){ 
     //var data = array(); 
     //data = document.forms["order_form3"].getElementsByTagName("input"); 
    } 
} 

ajaxRequest.send(data); 

}

+2

我不是經典的AJAX的好,但是,'readyState的== 4'是請求發送成功後,IIRC。那麼,你的'ajaxRequest.send(data);'行應該在響應行之前? – TheDeadLike

+0

我沒有看到任何數據發送! – pregmatch

回答

1

我建議使用像螢火蟲,甚至內置的Chrome或Firefox的功能插件,看看已發送以及已在您的文章的請求被接受。 ajax響應不會打印在屏幕上的某處,因爲您可能從「正常」的http POST請求到一個php頁面來了解它。它將作爲你的ajaxRequest對象的一部分返回,你可以從那裏獲取它來使用Javascript來「打印」它。

0

似乎你需要編碼你的參數,調用你的函數確實提交你的數據,但是他們沒有綁定到另一端的變量。我並不感到意外,PHP會拋棄你的數據。

在這裏看到:AJAX XMLHttpRequest POST