0

Object:

var obj = [{id: 1, name: "Peter", lastname: "Griffin"}]

Javascript:

$.ajax({
    method: 'post',
    url:'http://api/test.php',
    data: {     
        func: "addtodatabase",
        objectToBePassed: obj   //also tried JSON.stringify(obj)
    },          
    success: function(data) {
       //code here
    }
});

PHP:

$data = json_decode($_REQUEST['objectToBePassed'], true);

I can't seem to make it work. Help?

1
  • Have you inspected the value of $_REQUEST['objectToBePassed'] as you got it? And the value of $data after the decode? Your question lacks information about what goes wrong. Commented Sep 22, 2014 at 8:29

1 Answer 1

2

No, you seem to be confused on this. You do not need to json_decode() that. Once its in the post body. Just access it as it is:

$data = $_POST['objectToBePassed'];

Here is a demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.