Sharikov Vladislav
Sharikov Vladislav

Reputation: 7269

How to handle json data, which I send via ajax request, in php script?

I am sending some json data with ajax:

function send() {
    $.ajax({
        url: '/index.php?action=setShopOrdersGoods&order_id='+orderId,
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $('#target').html(data.msg);
        },
        data: JSON.stringify(goods)
    });
}

There are no problems with it. Firebug console screen:

enter image description here

Soajax request is sending okay. Now I need to handle it.

How I can do this?

echo __FILE__;
echo '<pre>';
var_dump($_POST);
echo '</pre>';
exit;

This code shows nothing. Looks like there are no data send via post. Firebug response tab of sent ajax request:

enter image description here

How I can handle json data in php file then?

Upvotes: 1

Views: 243

Answers (1)

Samar Haider
Samar Haider

Reputation: 912

Json data does not receive in post.

$json = file_get_contents('php://input');
$post = json_decode($json, TRUE);

echo __FILE__;
echo '<pre>';
var_dump($post);
echo '</pre>';
exit;

Upvotes: 3

Related Questions