使用POST发送JSON对象

I want to send a JSON object with POST

var user = {
    "first_name": first_name.value,
    "last_name": last_name.value,
    "age": age.value,
    "email": email.value
};   

This is how I send the object :

var request;
var url = 'ajax_serverside_JSON.php';
var destination;

function ajax_call(src, jsonObj, dst, method) {

    this.src = src;
    destination = dst;
    this.objJSON = jsonObj;
    this.request = getXMLHttpRequest();
    this.request.onreadystatechange = getResponse;
    if (method == 'POST') {
        sendPostRequest(objJSON);
    }
    return;
}

function sendPostRequest(objJSON) {
    request.open('POST', url, true);
    request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
    request.send(JSON.stringify(objJSON));
    return;
}

function getXMLHttpRequest() {
    try {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera,Safari
            request = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    } catch (e) {
        alert('The browser doesn\'t support AJAX: ' + e);
        request = false;
    }
    return request;
}

function getResponse() {
    if (request.readyState == 4 && request.status == 200) {
        document.getElementById(destination).innerHTML += request.response;
    }
}

Question: how do I read the object in the server side php ? What is the key o the sent json ?

echo var_dump ( $_POST ); shows

  array (size=0)
  empty

What is the problem?

AFTER EDIT

I changed my code to :

this.objJSON = jsonObj;

and

request.send("user=" + JSON.stringify(this.objJSON));

I try reading the JSON obj :

<?php
if (isset ( $_POST ['user'] )) {
    $obj = json_decode ( $_POST ['user'] );
    echo "isset";
} else {
    echo "is not set ";
    echo var_dump ( $_POST );
}
?>

...and still the same thing :

is not set

array (size=0)
  empty

Breaking your function down to its core 3 lines:

function ajax_call(src, jsonObj, dst, method) {

jsonOBJ is set in local scope as a variable.

this.objJSON = jsonObj;

jsonOBJ is copied to this.objJSON.

sendPostRequest(objJSON);

There is still no local variable objJSON since Javascript does not automatically consider this part of the local scope, hence null is sent, hence the empty $_POST.

Send jsonOBJ or this.objJSON instead and it'll work fine.

Try :

request.send("obj="+JSON.stringify(objJSON));

its a key value pair, I guess you are missing key.

I'm not sure that PHP can automatically parse JSON into $_POST variables. You should put the JSON into the value of a normal POST variable:

request.send('data=' + encodeURIComponent(JSON.stringify(objJSON));

and use the default Content-Type. Then in PHP you can do:

var_dump(json_decode($_POST['data']));

You also need to fix a Javascript variable, as explained in Niels Keurentjes's answer.

<script>
var user = {
                "first_name" : first_name.value,
                "last_name" : last_name.value,
                "age" : age.value,
                "email" : email.value
            };

$.customPOST = function(data,callback){
  $.post('your_php_script.php',data,callback,'json');
}

$(document).ready(function() {
    //suppose you have a button "myButton" to submit your data
    $("#myButton").click(function(){
        $.customPOST(user,function(response){
         //on the server side you will have :
         //$_POST['first_name'], $_POST['last_name'], .....
         //server will return an OBJECT called "response"
         //in "index.php" you will need to use json_encode();
         //in order to return a response to the client
         //json_encode(); must have an array as argument
         //the array must be in the form : 'key' => 'value'
        });
        return false;
    });
</script>

For your original Ajax request, you'll have to read the request body from php://input and decode then it.

<?php
$post = json_decode(file_get_contents('php://input'));
if (isset ( $post ['user'] )) {
    echo "isset";
} else {
    echo "is not set ";
    echo var_dump ( $post );
}