Vishnu G S
Vishnu G S

Reputation: 714

Retrieving objects from JSON using PHP

I need to send some data stored in IndexedDB to server for some back-end manipulation. The needed data is fetched to a variable payLoad in javascript using JSON.stringify().

 payLoad = "[{"synch":0,"id":-1,"name":"Tester","email":"[email protected]","created":"2014-08-20T07:56:44.201Z"}]";
    $.ajax({
                type: "POST",
                url: "process.php",
                data: payLoad,      // NOTE CHANGE HERE
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    alert(msg);
                },
                error: function(msg) {
                alert('error');
                }

            });

Can I parse this JSON data to a PHP class?

Upvotes: 0

Views: 62

Answers (1)

Amadan
Amadan

Reputation: 198324

This way, you're just sending JSON raw in the body. Try this:

$data = json_decode(file_get_contents('php://input'));

If, on the other hand, you send data with this:

data: { data: payLoad },

Then you can simply do

$data = json_decode($_POST['data']);

Upvotes: 3

Related Questions