首页 > 解决方案 > 在php中提取json

问题描述

你好,

那里有我的ajax代码

//If chkArray is not empty show the <div> and create the list
        if(chkArray.length !== 0) {
            $.ajax({
                method: 'POST',
                url : 'http://localhost/shop/ext/ajax/products_compare/test.php',
//                data : JSON.stringify({product_id: chkArray})
                data: {product_id: JSON.stringify(chkArray)},
            });
        }

我有这样的结果:

array(1) {
  ["{"product_id":"]=>
  array(1) {
    [""10","9""]=>
    string(0) ""
  }
}

如何在 php 中提取此代码?

标签: phpjsonajax

解决方案


无需使用 JSON.stringify,只需传递原始值数组:

$.ajax({
    method: 'POST',
    url : 'http://localhost/shop/ext/ajax/products_compare/test.php',
    data: {product_id: chkArray},
});

在服务器端,您将拥有$_POST['product_id']带有值的数组。


推荐阅读