PHP中的json_decode为有效json返回null
我从jquery提交的表单如下:PHP中的json_decode为有效json返回null,php,jquery,json,Php,Jquery,Json,我从jquery提交的表单如下: var submitForm = $('<form></form>').attr("action", "proceed.php").attr("method", "post"); var input = $('<input>').attr("type", "hidden").attr("name", "jsondata");//.attr("val
var submitForm = $('<form></form>').attr("action", "proceed.php").attr("method", "post");
var input = $('<input>').attr("type", "hidden").attr("name", "jsondata");//.attr("value", "");
var data = [];
elmProductSelected.children("option").each(function(n) {
var pIdx = $(this).attr("rel");
var pObj = products[pIdx];
var pname = pObj.manufacturer + " " + pObj.name;
var pdesc = pObj.description;
var pprice = pObj.price;
var pimg = pObj.img;
var jsonProd = new Object();
jsonProd.pname = pname;
jsonProd.pdesc = pdesc;
jsonProd.pprice = pprice;
jsonProd.pimg = pimg;
data.push(jsonProd);
});
input.attr("value",data);
submitForm.append(input);
submitForm.appendTo(document.body).submit();
if(isset($_POST["jsondata"])) {
echo "data received : ";
$data = json_decode($_POST["jsondata"], true);
var_dump($data);
} else {
echo 'no data received';
die();
}
[
{
"pname": "Yamaha AES1500 ELECTRIC GUITAR ORANGE STAIN / CASE",
"pdesc": "AES1500 combines the classic look and tone from the golden era of full-bodied rock with the modern advantages of precision engineering. Offered with the option of the famous Bigsby vibrato tailpiece, the AES1500 combines the character of classic American archtop-electric design with Yamaha high quality construction. The semi-thinline AES1500, a classy, comfortable archtop cutaway design, is an instrument that successfully straddles the rock, country and jazz genres. The two custom Q100 DiMarzio humbuckers possess a powerful warmth perfectly balanced by the clear, well defined cut-through of the maple body construction. Moreover, each pickup can be coil split, which in conjunction with the three-way pickup selector lends a whole new dimension of tonal versatility and subtlety.",
"pprice": 207101.43,
"pimg": "5b2f8d703c899290cbcbe325058dfaeb"
},
{
"pname": "Yamaha AES1500 ELECTRIC GUITAR RED STAIN / CASE",
"pdesc": "AES1500 combines the classic look and tone from the golden era of full-bodied rock with the modern advantages of precision engineering. Offered with the option of the famous Bigsby vibrato tailpiece, the AES1500 combines the character of classic American archtop-electric design with Yamaha high quality construction. The semi-thinline AES1500, a classy, comfortable archtop cutaway design, is an instrument that successfully straddles the rock, country and jazz genres. The two custom Q100 DiMarzio humbuckers possess a powerful warmth perfectly balanced by the clear, well defined cut-through of the maple body construction. Moreover, each pickup can be coil split, which in conjunction with the three-way pickup selector lends a whole new dimension of tonal versatility and subtlety.",
"pprice": 207101.43,
"pimg": "5b2f8d703c899290cbcbe325058dfaeb"
}
]
我从服务器上得到的是:
data received : NULL
我的JSON是有效的,用http://jsonlint.com/
它看起来是这样的:
var submitForm = $('<form></form>').attr("action", "proceed.php").attr("method", "post");
var input = $('<input>').attr("type", "hidden").attr("name", "jsondata");//.attr("value", "");
var data = [];
elmProductSelected.children("option").each(function(n) {
var pIdx = $(this).attr("rel");
var pObj = products[pIdx];
var pname = pObj.manufacturer + " " + pObj.name;
var pdesc = pObj.description;
var pprice = pObj.price;
var pimg = pObj.img;
var jsonProd = new Object();
jsonProd.pname = pname;
jsonProd.pdesc = pdesc;
jsonProd.pprice = pprice;
jsonProd.pimg = pimg;
data.push(jsonProd);
});
input.attr("value",data);
submitForm.append(input);
submitForm.appendTo(document.body).submit();
if(isset($_POST["jsondata"])) {
echo "data received : ";
$data = json_decode($_POST["jsondata"], true);
var_dump($data);
} else {
echo 'no data received';
die();
}
[
{
"pname": "Yamaha AES1500 ELECTRIC GUITAR ORANGE STAIN / CASE",
"pdesc": "AES1500 combines the classic look and tone from the golden era of full-bodied rock with the modern advantages of precision engineering. Offered with the option of the famous Bigsby vibrato tailpiece, the AES1500 combines the character of classic American archtop-electric design with Yamaha high quality construction. The semi-thinline AES1500, a classy, comfortable archtop cutaway design, is an instrument that successfully straddles the rock, country and jazz genres. The two custom Q100 DiMarzio humbuckers possess a powerful warmth perfectly balanced by the clear, well defined cut-through of the maple body construction. Moreover, each pickup can be coil split, which in conjunction with the three-way pickup selector lends a whole new dimension of tonal versatility and subtlety.",
"pprice": 207101.43,
"pimg": "5b2f8d703c899290cbcbe325058dfaeb"
},
{
"pname": "Yamaha AES1500 ELECTRIC GUITAR RED STAIN / CASE",
"pdesc": "AES1500 combines the classic look and tone from the golden era of full-bodied rock with the modern advantages of precision engineering. Offered with the option of the famous Bigsby vibrato tailpiece, the AES1500 combines the character of classic American archtop-electric design with Yamaha high quality construction. The semi-thinline AES1500, a classy, comfortable archtop cutaway design, is an instrument that successfully straddles the rock, country and jazz genres. The two custom Q100 DiMarzio humbuckers possess a powerful warmth perfectly balanced by the clear, well defined cut-through of the maple body construction. Moreover, each pickup can be coil split, which in conjunction with the three-way pickup selector lends a whole new dimension of tonal versatility and subtlety.",
"pprice": 207101.43,
"pimg": "5b2f8d703c899290cbcbe325058dfaeb"
}
]
如果我不发送此数据,而是发送以下内容:
var a ="{\"id\":\"1\"}";
input.attr("value",a);
服务器成功获取json:
data received : array(1) { ["id"]=> string(1) "1" }
问题:第一个请求可能有什么问题
谢谢。输入.attr(“值”,数据)代码>设置默认值,而不是当前值。要设置当前值,请使用.val()
此外,问题的真正原因是您拥有的是一个对象,而不是json。如果要将其作为json发送,则应将其转换为json,否则实际上会发送无效的json[object object,object object]
input.val(JSON.stringify(data));
请发布完整的PHP代码(一个有问题的工作示例)代码>在json_解码之前?因为解码你发布的json对Metat是有效的。转储接收到的JSON,而不是调试此代码的解码结果。@DonRhummy,这是完整的代码。@NielsKeurentjes他正在向服务器发送(或试图发送)JSON,而不是从服务器接收。是的,就是这样。谢谢