Sates
Sates

Reputation: 408

Passing JSON to PHP

This is my function in javascript:

function callRemoteService(requestId, sObjectId, bObjectId) {
$.ajax({
    url: "../../../serviceRemoteEngine.php",
    dataType: "json",
    contentType: "application/json",
    type: "POST",
    timeout: 1000,
    data: JSON.stringify({"requestId":requestId,"SOobjectId":sObjectId,"SBobjectId":bObjectId}),
    success: function(remoteResponse){
        alert(remoteResponse.msg);
    }
});
}

And this is serviceRemoteEngine.php:

echo json_encode(array("msg" => $_POST["SOobjectId"]));

The function is called with these parameters:

callRemoteService('remove', 15, 0)

The thing is that, instead of seeing 15 in alert message, null is displayed instead.

However, when I change PHP file into:

echo json_encode(array("msg" => "message"));

"message" text is displayed with js alert.

Why?

Upvotes: 0

Views: 68

Answers (3)

Giacomo1968
Giacomo1968

Reputation: 26066

Your PHP for the JSON is this:

echo json_encode(array("msg" => "message"));

But you should see if adding proper JSON headers will help clear things up. Like this:

$json_data = json_encode(array("msg" => "message"));
header('X-JSON: (' . $json_data . ')');
header('Content-type: application/x-json');
echo $json_data;

Also, reading over your question again, you seem to be wanting to get the $_POST["SOobjectId"] immediately & then sent that back via JSON? Are you sure that $_POST contains anything? In your PHP file before you do anything else, do this:

echo '<pre>';
print_r($_POST);
echo '</pre>';

Or do this for the $_REQUEST to see if data is transmitted:

echo '<pre>';
print_r($_REQUEST);
echo '</pre>'; 

Perhaps you need to do this in your PHP file. Get the raw $_POST data via file_get_contents('php://input') and then decode it and handle it that way. All of my reworking—including this idea—below:

$json_decoded_array = json_decode(file_get_contents('php://input'), true);

$json_data = json_encode(array("msg" => $json_decoded_array['SOobjectId']));
header('X-JSON: (' . $json_data . ')');
header('Content-type: application/x-json');
echo $json_data;

Upvotes: 0

Yaroslav Admin
Yaroslav Admin

Reputation: 14535

You don't need to call JSON.stringify(), when sending ajax-request, because $.ajax() function expects associative array of parameters, not string.

function callRemoteService(requestId, sObjectId, bObjectId) {
    $.ajax({
        url: "../../../serviceRemoteEngine.php",
        dataType: "json",
        contentType: "application/json",
        type: "POST",
        timeout: 1000,
        data: {"requestId":requestId,"SOobjectId":sObjectId,"SBobjectId":bObjectId},
        success: function(remoteResponse){
            alert(remoteResponse.msg);
        }
    });
}

Upvotes: 2

Marc B
Marc B

Reputation: 360672

PHP expects a post/get request to have key=value pairs. You're sending over a bare string, so it's just value. Since there's no key, PHP cannot (and will not) put anything into $_POST, since there's no key to attach the value to.

Try

data: {foo: JSON.stringify(...)}

and

echo $_POST['foo']

instead.

Upvotes: 1

Related Questions