dougongnan2167 2015-07-21 15:46
浏览 59

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.

  • 写回答

1条回答 默认 最新

  • dousuiguang9328 2015-07-21 16:04
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧