[英]Sending an array of objects to PHP function with jQuery.ajax request
There is an array of objects like this: 有这样的对象数组:
rectangle[0].width = w;
rectangle[0].height = h;
rectangle[1].width = w;
rectangle[2].height = h;
rectangle[3].width = w;
rectangle[3].height = h;
...
How we may to send this array to PHP function with jQuery.ajax request and vise versa, modified array from PHP function as response? 我们如何通过jQuery.ajax请求将此数组发送到PHP函数,反之亦然,如何将来自PHP函数的修改数组作为响应? I mind that JS code may be: 我认为JS代码可能是:
request = $.ajax({
type : "POST",
url : "post.php",
data : {rec :rectangle}
});
request.done(function(msg) {
alert(msg);
});
request.fail(function(jqXHR, textStatus) {
alert("Function inaccessible: " + textStatus)
});
and PHP: 和PHP:
if (isset($_POST["rec"]) {
$rec = $_POST["rec"];
$arr_length = count($rec);
$response = $arr_length;
}
echo $response;
Please, demonstrate the true form of request. 请证明请求的真实形式。 Thanks. 谢谢。
Very easy: 很容易:
<script>
var myarray = new Array();
var params = { myarray: myarray };
var paramJSON = JSON.stringify(params);
$.post(
'test.php',
{ data: paramJSON },
function(data) {
var result = JSON.parse(data);
}
</script>
php side:
if(isset($_POST["data"]))
{
$data = json_decode($_POST["data"]);
$myarray = $data->myarray;
foreach($myarray as $singular)
{
// do something
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.