ajax和json对象编码和解码进入PHP脚本

i have created a simple html file with a fixed json object. I want to take the object to the php file text.php, encode it, decode it, print it in the php file, and then print it back in the html file. html file:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
    <script>
        var key=45;
        var value=65;
        var rated = {"key" : key , "value" : value};
        var rated_encoded = JSON.stringify(rated);

        $.ajax({
            type: "POST",
            url: "text.php",
            dataType:"json",
            data: {
                "rated" : rated_encoded
            },
            //success: function(data) {
                //document.write(data);
            //}
        });
    </script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("#div1").load("text.php");
            });
        });
    </script>
    <div id="div1"><h6>Result</h6></div>

    <button>Get External Content</button>

</body>
</html>

text.php code:

<?php 
if(isset($_POST["rated"])){
    $rated_json = $_POST["rated"];
    $JSONArray  = json_decode($rated_json, true); 
    if($JSONArray !== null){ 
        $key = $JSONArray["key"];
        $value = $JSONArray["value"];
    }
    echo json_encode($rated_json);
} 
?>

the

echo json_encode($rated_json);

in the last line of text.php is not working. I don't know if we can't print encoded stuff or something. In place of that line, even if i type echo "hello";, it's not printing in the php file. If there is any way i can print the $JSONArray variable in text.php, that would be great. It would be better if i can encode the json object in the html file itself, send to the php script, decode it there and then send back and print in the html file. Sorry if this question is not worded properly or is very basic. Also, please go slow and explain the code.

You need add contentType: "application/json; charset=UTF-8",

$.ajax({
            type: "POST",
            url: "text.php",
            dataType:"json", 
            contentType: "application/json; charset=UTF-8",
            data: {
                "rated" : rated_encoded
            },
            //success: function(data) {
                //document.write(data);
            //}
        });

the dataType option is just for parsing the received data.

If still not working, add this: processData: false