Javascript和PHP。使用Ajax将多个数组发送到PHP并将其转换为PHP数组
我需要一些帮助。我正试图直接向php发送宽度和长度的乘法数组。我不想将它保存到任何HTML字段,但是它不工作。我从html文本中获取宽度和长度,并将其转换为数字,然后将其添加到javascript中的数组中 这是代码Javascript和PHP。使用Ajax将多个数组发送到PHP并将其转换为PHP数组,javascript,php,Javascript,Php,我需要一些帮助。我正试图直接向php发送宽度和长度的乘法数组。我不想将它保存到任何HTML字段,但是它不工作。我从html文本中获取宽度和长度,并将其转换为数字,然后将其添加到javascript中的数组中 这是代码 var widthL = []; var lengthL = []; var widths = document.wall.width.value; var lengths = document.wall.length.value; var wNumber = Number(w
var widthL = [];
var lengthL = [];
var widths = document.wall.width.value;
var lengths = document.wall.length.value;
var wNumber = Number(widths);
var lNumber = Number(lengths);
widthL.push(JSON.stringify(wNumber));
lengthL.push(JSON.stringify(lNumber));
这是我用来将其发送到PHP的Ajax代码
$.ajax( {
type: "POST",
url: "./Summary.php",
data: {"widths": widthL, "lengths" : lengthL},
cache: false,
success: function (response) {
console.log("This is the width", widthL, " This is the length", lengthL);
}
});
在PHP中,我使用此代码来接收它。但我不会把东西拿回来
<?php
$lengths = json_decode($_POST['lengths']);
$widths = json_decode($_POST['widths']);
echo 'This is the width: '.$widtsL;
echo 'This is the length: '.$lengths;
?>
我希望有人能在这里帮助我。首先,您应该在ajax帖子中指定内容类型:
$.ajax( {
type: "POST",
url: "./Summary.php",
contentType: "application/json; charset=UTF-8", // Add content type
data: {"widths": widthL, "lengths" : lengthL},
cache: false,
success: function (response) {
console.log("This is the width", widthL, " This is the length", lengthL);
}
});
然后在PHP中:
$request_body = file_get_contents('php://input'); //This reads the raw POST data
$json = json_decode($request_body); // Then parse it to JSON
$lengths = $json->lengths;
$widths = $json->widths;
请将POST参数名称添加为数据类型,键入值JSON, Ajax数据参数值使用key=value&key=value格式
然后在php文件中输入debug code如果
var\u dump($\u POST[]),您会得到什么代码>?在进行AJAX调用之前,不要使用JSON.stringify()
之类的东西。如果您将dataType:“json”
添加到ajax调用中,那么您就可以解决所有这些问题。添加dataType:“json”
到您的$.ajax()
调用中。当我使用var\u dump($\u POST['length')时。我得到NULLtryvar\u dump(json\u decode)(文件\u get\u内容(“php://input")));代码>我已经尝试了你的代码和回声出来,但我仍然没有得到任何回报