jQuery $.post() JSON Object

风格不统一 提交于 2019-12-06 15:26:20

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 { ... }
});

Are you sure your server is parsing it properly? The fact that it gets that far implies that the issue is in your 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).

You could do something like this:

  1. Use the Javascript Object Method JSON.parse
  2. Set That to a Certain POST value, we'll say "json"
  3. Read it and decode on the server. With PHP, this would something like 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:

$jsonDecoded = json_decode($_POST['json'])

Use json as the forth parameter.

$.post('ui-DashboardWidgetsPost.php', dashboardJSON, function(msg)
    {
        if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
        else
        {
        }
    }, 'json');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!