2013-07-23 183 views
1

我在嘗試通過發佈數據通過ajax請求發送參數變量時遇到了很多麻煩,我無法讓它工作出於某種奇怪的原因。當我使用實際變量包含的內容時,沒有問題,但我不能這樣做,因爲它每次都會改變,所以不是使用硬編碼的東西,我想嘗試使用變量,它包含的字符的charset總是儘管如此。在通過ajax請求發送發佈數據的參數中使用變量

var http=new XMLHttpRequest(); 
uri='http://www.example.com/profile.php'; 
http.open('POST',uri,true); 
params='name=Tom&lastname=Jordan&'+variable+'='+secondvariable+'; 
http.setRequestHeader('Content-type','application/x-www-form-urlencoded'); 
http.setRequestHeader('Content-length',params.length); 
http.setRequestHeader('Connection', 'close'); 
http.onreadystatechange=function(){ 
if(http.readyState==4&&http.status==200){ 
//alert(http.responseText); 
} 
}; 
http.send(params); 

什麼是正確的語法,包括一些變量和一些純文本?或者乾脆如何做這項工作?

回答

2

你有一個流浪報價:

變化:

params='name=Tom&lastname=Jordan&'+variable+'='+secondvariable+'; 

到:

params='name=Tom&lastname=Jordan&'+variable+'='+secondvariable; 

此外,確保逃生您變量正確:

params='name=Tom&lastname=Jordan&'+variable+'='+encodeURIComponent(secondvariable); 
+0

好的,我現在就試試看,並告訴你我是否有任何問題。 – bob

+0

是的,它的工作原理。謝啦 :)。 – bob

0

您有一個額外的單引號:

params='name=Tom&lastname=Jordan&'+variable+'='+secondvariable+'; 

+'末是沒有必要的,它的治療分號爲字符串。 (可能導致一個語法錯誤。)

相關問題