0

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?

2
  • 4
    json_decode() Commented Aug 29, 2014 at 6:02
  • Can I know the code please? Additionally, how can I obtain this JSON String in PHP using $_POST[]? Commented Aug 29, 2014 at 6:03

1 Answer 1

3

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']);
Sign up to request clarification or add additional context in comments.

1 Comment

The key point of the answer lies in the first code snippet in this answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.