Json stringify to php

I want to pass key values ​​to php page.

On the php page, I will start reading the value by matching ajaxcallid.

But it does not work.

It should do with the syntax / method that I pass, causing an error.

parse error
invalid json: ajax call id is missing    

JavaScript / AJAX:

var person = { 
     "Address"    :   "123 Anywhere St.", 
     "City"       :   "Springfield", 
     "PostalCode" :   99999
};

alert(person);          

person= JSON.stringify(person);

alert(person);

$.ajax({
    url: 'ROOT_URL/admin/ajaxtest.php',
    type: "POST",
    dataType: 'json',
    data: {ajaxcallid: '26', jsarr: person},
    timeout: 5000,
    success:  function(output) {
        alert(output.Address);
    },
});

PHP:

<?php
if (isset($_REQUEST['ajaxcallid']))
{    
    if($_REQUEST['ajaxcallid']==26)
    {    
        //example, I want to read value of person.Address, person.City, 
        //person.PostalCode
    //what is the easiest way
        $phparr= json_decode($_REQUEST['jsarr']);
        //do all other operation
        $output= json_encode($phparr);
    }
}
else
{
    $output= "ajax call id is missing";
}
echo $output;
?>
+5
source share
5 answers

If you are not using dataType: 'json', you may need to do stripslashes

$.post(window.data.baseUrl, {posts : JSON.stringify(posts)});

And in php:

$posts = json_decode(stripslashes($_POST['posts']));
+17
source

It helps me:

 data = json_decode($this->request->data['jsarr'], true);

in your php code to access write

Hope this helps someone!

+6
source

, -. , JQuery . , = JSON.stringify(). .

+5

, $.ajax PHP :

JQuery

$.ajax({
    url: "/admin/ajaxtest.php",
    method: "POST",
    data: {
        ajaxcallid: "26",
        person: JSON.stringify({
            "Address" : "123 Anywhere St.",
            "City" : "Springfield",
            "PostalCode" : "99999"
        })
    }
}).done(function(data) {
    if (!data) {
        // generic error message here
    } else if (data == 'invalid') {
        alert('no ajaxcallid received');
    } else {
        var result = $.parseJSON(data); // if you pass back the object
        alert(result.Address);
    }
});

PHP

if (isset($_REQUEST['ajaxcallid'])) {
    if ((int) $_REQUEST['ajaxcallid'] == 26) {
        $personData = json_decode($_REQUEST['person']);
        $address = $personData->Address;
        $postalCode = $personData->PostalCode;
        $returnData = json_encode($personData);
        echo $personData;
        die();
    }
} else {
    echo 'invalid';
    die();
}
+3

I have not worked with PHP, but in my experience with the ASP.netfollowing may help you.

Add key contentTypein ajax settigns:

type: "POST",
contentType:'application/json',
dataType: 'json',

I also think that you need stringifyan integer value that you assign dataas follows:

var person = { 
     "Address"    :   "123 Anywhere St.", 
     "City"       :   "Springfield", 
     "PostalCode" :   99999
};

var d= {ajaxcallid: '26', jsarr: person};
var dat=JSON.stringify(d);


......
data: dat,
......
0
source

All Articles