& php post receive problem
my ajax request is
ajaxRequest.send("var1=" + var1.value + "&var2=" + var2.value + "&var3=" + var3.value);
but the browser sends the request as
var1= hello&var2= world&a开发者_JS百科mp;var3=1
php side receives the data as a post
$variable1 = $_POST['var1'];
$variable2 = $_POST['var2'];
$variable3 = $_POST['var3'];
only the first variable receives the data. The las two variables do not receive any data.
the correct format of sending the request as i know is
var1= hello&var2= world&var3=1
But why did the browser appended some useless characters that made the php side unable to recognise what has been sent ?
The problem is your ampersands are encoded before sending the request to the browser. This way, the next variable is not recognized. Instead, the first variable contains: hello&var2= world&var3=1
. I don't know which javascript library you're using, but it seems to me either you are using the wrong function arguments (maybe you need to inject an object instead of a string?), or the function wrongly encodes the string.
Take jQuery's ajax method for example, query parameters are passed as an object, like this:
var data = { 'var1' : var1.value, 'var2' : var2.value };
$.ajax('/ajax/', { 'data': data});
What happen's when you use urlencode ?
You could put the data string into a variable first then urlencode that veriable and pass the variable inside the ajaxRequest.send
精彩评论