jQuery $ .post()JSON对象 [英] jQuery $.post() JSON Object
问题描述
我有一个JSON对象
{
"widgetSettings":[{"maxDisplay": 6, "maxPerRow": 2}],
"widgets": [
{"wigetID": 1, "show": false, "weight": 0, "widgetTitle": "Widget 1", "widgetColor": "defualt"},
{"wigetID": 2, "show": false, "weight": 0, "widgetTitle": "Widget 2", "widgetColor": "defualt"},
{"wigetID": 3, "show": false, "weight": 0, "widgetTitle": "Widget 3", "widgetColor": "defualt"},
{"wigetID": 4, "show": false, "weight": 0, "widgetTitle": "Widget 4", "widgetColor": "defualt"},
{"wigetID": 5, "show": false, "weight": 0, "widgetTitle": "Widget 5", "widgetColor": "defualt"},
{"wigetID": 6, "show": false, "weight": 0, "widgetTitle": "Widget 6", "widgetColor": "defualt"},
{"wigetID": 7, "show": false, "weight": 0, "widgetTitle": "Widget 7", "widgetColor": "defualt"},
{"wigetID": 8, "show": false, "weight": 0, "widgetTitle": "Widget 8", "widgetColor": "defualt"},
{"wigetID": 9, "show": false, "weight": 0, "widgetTitle": "Widget 9", "widgetColor": "defualt"},
{"wigetID": 10, "show": false, "weight": 0, "widgetTitle": "Widget 10", "widgetColor": "defualt"},
{"wigetID": 11, "show": false, "weight": 0, "widgetTitle": "Widget 11", "widgetColor": "defualt"},
{"wigetID": 12, "show": false, "weight": 0, "widgetTitle": "Widget 12", "widgetColor": "defualt"},
{"wigetID": 13, "show": false, "weight": 0, "widgetTitle": "Widget 13", "widgetColor": "defualt"},
{"wigetID": 14, "show": false, "weight": 0, "widgetTitle": "Widget 14", "widgetColor": "defualt"},
{"wigetID": 15, "show": false, "weight": 0, "widgetTitle": "Widget 15", "widgetColor": "defualt"},
{"wigetID": 16, "show": false, "weight": 0, "widgetTitle": "Widget 16", "widgetColor": "defualt"}
]}
我想使用jQuery将其发布到服务器端脚本,以便可以将其保存在数据库中.当我说保存时,我的意思是作为JSON对象.但是,当您以其他方式发布信息/获取信息时,看到的是我发布的JSON JSON格式的JSON格式,这样我可以保存在数据库中,看起来像是迷路了,数据库留下了空值.关于我可能做错了的任何想法..这是jQuery部分.
I want to with jQuery to post that to a server side script so I can save it in a DB. And when I say save it I mean as the JSON object. However seeing as when you make a post/get other the way its posted is a JSON format my JSON that I am posting so I can save in the DB, gets lost it seems and the DB gets left with an empty value. Any ideas to what I may be doing wrong.. heres the jQuery portion.
$.post('ui-DashboardWidgetsPost.php', {"dashWidgets":dashboardJSON}, function(msg)
{
if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
else
{
}
});
编辑 PHP
EDIT The PHP
<?php
$validJSON = $_POST['dashWidgets'];
mysql_connect("127.0.0.1", "", "") or die('{"error": "yes", "errormsg": "'.mysql_error().'"}');
mysql_select_db("xxxx") or die('{"error": "yes", "errormsg": "'.mysql_error().'"}');
$result = mysql_query("UPDATE dashboardPrefs SET widgetSettings='".$validJSON."' WHERE userID=100")
or die('{"error": "yes", "errormsg": "'.mysql_error().'"}');
echo '{"error": "none"}';
?>
推荐答案
如果要将JSON(作为字符串,而不是实际值)发送给DB,也许应该将其视为一个?
If you want to send the JSON (as string, not the actual values) to the DB, perhaps you should treat it as one?
$.post('ui-DashboardWidgetsPost.php', {
json: dashboardJSON
}, function(msg) {
msg=jQuery.parseJSON(msg);
if (msg.error == "yes") {
console.log('Error Found: ' + msg.errorMsg);
} else { ... }
});
这篇关于jQuery $ .post()JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!