1

I'm having a lot of trouble trying to send a variable through the parameters through post data via an ajax request, I cannot get it to work for some odd reason. When I use what the actual variable contains, there is no problem, but I cannot do that since it changes every time, so instead of using something hard-coded I wanted to try using a variable, the charset of the characters it contains is always the same though.

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);

What is the correct syntax to include a variable alongside with some plain-text? Or simply how can I make this work?

4

2 回答 2

2

你有一个流浪报价:

改变:

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

至:

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

此外,请确保正确转义您的变量:

params='name=Tom&lastname=Jordan&'+variable+'='+encodeURIComponent(secondvariable);
于 2013-07-23T22:19:09.987 回答
0

你有一个额外的单引号:

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

最后+'的 the 不是必需的,它将分号视为字符串。(可能会导致语法错误。)

于 2013-07-23T22:18:53.253 回答