我正在从jquery提交表单:
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();
在proceed.php
我试图像这样得到这个JSON:
if(isset($_POST["jsondata"])) {
echo "data received : ";
$data = json_decode($_POST["jsondata"], true);
var_dump($data);
} else {
echo 'no data received';
die();
}
我从服务器得到的是:
data received : NULL
我的JSON是有效的,使用http://jsonlint.com/
进行测试,它看起来像这样:
[
{
"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" }
问题:首次请求可能出现什么问题?
感谢。
答案 0 :(得分:2)
input.attr("value",data);
设置默认值,而不是当前值。要设置当前值,请使用.val()
。
此外,问题的真正原因是你拥有的是一个对象,而不是json。如果你想把它作为json发送,你应该将它转换为json,否则你实际上会发送无效的json [object Object, object Object]
input.val(JSON.stringify(data));