2013-07-23 143 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+'; 

+'末是没有必要的,它的治疗分号为字符串。 (可能导致一个语法错误。)

相关问题