javascript jQuery $.post() JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7137464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery $.post() JSON Object
提问by chris
I have a 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"}
]}
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.
我想使用 jQuery 将其发布到服务器端脚本,以便我可以将其保存在数据库中。当我说保存时,我的意思是作为 JSON 对象。但是,当您发布/获取其他内容时,其发布方式是 JSON 格式,我发布的 JSON 以便我可以保存在数据库中,但似乎丢失了,并且数据库留下了一个空值。我可能做错了什么的任何想法..继承人的 jQuery 部分。
$.post('ui-DashboardWidgetsPost.php', {"dashWidgets":dashboardJSON}, function(msg)
{
if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
else
{
}
});
EDITThe PHP
编辑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"}';
?>
回答by Robus
If you want to send the JSON (as string, not the actual values) to the DB, perhaps you should treat it as one?
如果您想将 JSON(作为字符串,而不是实际值)发送到数据库,也许您应该将其视为一个?
$.post('ui-DashboardWidgetsPost.php', {
json: dashboardJSON
}, function(msg) {
msg=jQuery.parseJSON(msg);
if (msg.error == "yes") {
console.log('Error Found: ' + msg.errorMsg);
} else { ... }
});
回答by John Green
Are you sure your server is parsing it properly? The fact that it gets that far implies that the issue is in your PHP.
你确定你的服务器能正确解析它吗?事实如此之远意味着问题出在您的 PHP 中。
You're also best off also making sure that the data is moving across the wire properly, which you can do through the network tab of Chrome/Firebug. That being said, I prefer to use an external packet sniffer like Fiddler (or HTTPScoop on the Mac).
您还最好确保数据正确地通过网络移动,您可以通过 Chrome/Firebug 的网络选项卡来完成。话虽如此,我更喜欢使用像 Fiddler(或 Mac 上的 HTTPScoop)这样的外部数据包嗅探器。
回答by FreezeWarp
You could do something like this:
你可以这样做:
- Use the Javascript Object Method JSON.parse
- Set That to a Certain POST value, we'll say "json"
- Read it and decode on the server. With PHP, this would something like json_decode($_POST['json']).
- 使用 Javascript 对象方法 JSON.parse
- 将它设置为某个 POST 值,我们会说“json”
- 读取它并在服务器上解码。使用 PHP,这将类似于 json_decode($_POST['json'])。
Thus, the code on the client might be:
因此,客户端上的代码可能是:
$.post('ui-DashboardWidgetsPost.php', 'json=' + JSON.parse(dashboardJSON), function(msg)
{
if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
else
{
}
});
And in PHP:
在 PHP 中:
$jsonDecoded = json_decode($_POST['json'])
回答by Kristiono Setyadi
Use json
as the forth parameter.
使用json
作为第四参数。
$.post('ui-DashboardWidgetsPost.php', dashboardJSON, function(msg)
{
if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
else
{
}
}, 'json');