简体   繁体   English

&PHP的帖子收到问题

[英]& php post receive problem

my ajax request is 我的ajax请求是

ajaxRequest.send("var1=" + var1.value + "&var2=" + var2.value + "&var3=" + var3.value); 

but the browser sends the request as 但浏览器将请求发送为

var1= hello&var2= world&var3=1

php side receives the data as a post php端接收数据作为发布

$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 ? 但是,为什么浏览器附加了一些无用的字符,使php端无法识别已发送的内容?

What happen's when you use urlencode ? 使用urlencode会发生什么?

You could put the data string into a variable first then urlencode that veriable and pass the variable inside the ajaxRequest.send 您可以先将数据字符串放入变量中,然后再输入可验证的urlencode并将该变量传递给ajaxRequest.send

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 . 相反,第一个变量包含: 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. 我不知道您使用的是哪个JavaScript库,但是在我看来,您使用的是错误的函数参数(也许您需要注入对象而不是字符串?),或者函数对字符串的编码错误。

Take jQuery's ajax method for example, query parameters are passed as an object, like this: jQuery的ajax方法为例,查询参数作为对象传递,如下所示:

var data = { 'var1' : var1.value, 'var2' : var2.value };
$.ajax('/ajax/', { 'data': data});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM