0

I am trying to send following javascript object using AJAX

var bodycontent = {
  search: search,
  query: query,
  start: start,
  show: show,
};

My AJAX call

$.ajax({
    type: "POST",
    data: dataString,
    cache: false,
    url: 'url.php',
    success: function(html) {
        alert(html);
    }
})

The variable dataString

var dataString = "bodycontent="+bodycontent;

I tried console.log for bodycontent to ensure it had data and the result is as follows

{search: "human", query: Array(1), start: 20, show: 40}

On PHP page I tried follow code to decode the object being sent

json_decode($_POST['bodycontent'],true)

But the value is empty. If I don't json_decode it returns [object Object]

4
  • have you looked at JSON.stringify to convert that Object literal to a string? Commented Aug 30, 2020 at 8:02
  • Try converting your object to string with data: JSON.stringify([dataString ]) Commented Aug 30, 2020 at 8:05
  • I have tried converting it to JSON.stringify but same result.. print_r(json_decode($_POST['bodycontent'],true) returns an empty alert Commented Aug 30, 2020 at 8:14
  • jQuery ajax expects data to be a live object, a string or an array. You're posting this string: "bodycontent=[object Object]", and that's not valid JSON, which your server expects to get. Commented Aug 30, 2020 at 8:15

2 Answers 2

3

You can't concatenate an object and a string

Just send the object as data and jQuery will serialize it internally

$.ajax({
    type: "POST",
    data: bodycontent ,
    cache: false,// pointless since POST doesn't get cached by browser
    url: 'url.php',
    success: function(html) {
        alert(html);
    }
})

Then in php look for the individual keys within your object like:

$_POST['search']  
$_POST['query']// will be array since that's what you are sending
etc
Sign up to request clarification or add additional context in comments.

Comments

1

If you use JSON.stringify you can prepare the object for transmission by casting as a string. The payload of the ajax request takes the name bodycontent as the parameter ( and you will look for that in the POSTed data ) and appends the stringified object literal data.

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        ob_clean();
        $body=json_decode($_POST['bodycontent']);
        exit( print_r( $body, true ) );
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
        <script src='//code.jquery.com/jquery-latest.js'></script>
        <script>
            // whatever this should be...
            var bodycontent = {
                search: 'human',
                query: [1,2,3,4,5,6],
                start: 20,
                show: 40
            };
            
            
            $.ajax({
                type:"POST",
                data:{ bodycontent:JSON.stringify( bodycontent ) },
                cache:false,
                url:'url.php',
                success:function(html) {
                    alert(html);
                }
            });
        </script>
    </head>
    <body>
        <!-- content -->
    </body>
</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.