JQuery $ .post () JSON Object

I have a JSON object

{ 
    "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 jQuery to post it to the server side script, so I can save it to the DB. And when I say Save, I mean a JSON object. However, seeing that when you make a message / receive another like its published JSON format, my JSON that I am sending so I can save it to the DB seems to get lost and the DB remains empty. Any ideas on what I might be doing wrong. Heres part of jQuery.

$.post('ui-DashboardWidgetsPost.php', {"dashWidgets":dashboardJSON}, function(msg)
    {
        if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
        else
        {
        }
    });

      

CHANGE 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"}';
?>

      

+1


source to share


4 answers


If you want to send JSON (as a string, not 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 { ... }
});

      

+4


source


Are you sure your server parses it correctly? The fact that it gets this far implies that the problem is in your PHP.



Your best bet is also to make sure the data travels over the cable correctly, which you can do via the Chrome / Firebug network tab. That being said, I prefer to use an external packet sniffer like Fiddler (or HTTPScoop on Mac).

+2


source


You can do something like this:

  • Use Javascript Object JSON.parse method
  • Set this value to a specific POST value, we'll say "json"
  • Read it and decrypt it on the server. From PHP it will be like json_decode ($ _ POST ['json']).

So the code on the client could 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'])

      

+2


source


Use json

as fourth parameter.

$.post('ui-DashboardWidgetsPost.php', dashboardJSON, function(msg)
    {
        if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
        else
        {
        }
    }, 'json');

      

+2


source







All Articles