Html从php获取json并解析

先上html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0-rc1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
    <div id="placeholder"></div>
    <script type="text/javascript">
    var jsonStr = "{\"total\":100,\"data\":[{\"id\":10001,\"name\":\"scott\"},{\"id\":10002,\"name\":\"tiger\"}]}";
    $(document).ready(function(){
        $.ajax({
            url:"http://localhost/default_search.php",
            type: "GET",
            dataType: "text",
            success: function(json){
                alert(json);//$a的值

                var jsonObj = window.JSON.parse(json);
                var str = "json字符串解析为json对象<br>";
                str += "<span>Total:"+jsonObj.total+"</span><br><span>Data:";
                for (var i=0;i<jsonObj.data.length ; i++)
                {
                    str += "id:" + jsonObj.data[i].id + ",name:" + jsonObj.data[i].name+"<br>";
                }
                str += "</span><br>";
                document.querySelector("#placeholder").innerHTML = str;
            }
 
        })
    })
    </script>
</body>
</html>

其中使用get方法调用php网页

    $(document).ready(function(){
        $.ajax({
            url:"http://localhost/default_search.php",
            type: "GET",
            dataType: "text",
            success: function(json){
                alert(json);//$a的值
            }
        })
    })

 解析json

var jsonObj = window.JSON.parse(json);

 解析的数据显示在网页

str += "<span>Total:"+jsonObj.total+"</span><br><span>Data:";
for (var i=0;i<jsonObj.data.length ; i++)
{
str += "id:" + jsonObj.data[i].id + ",name:" + jsonObj.data[i].name+"<br>";
}
str += "</span><br>";
document.querySelector("#placeholder").innerHTML = str;

 php文件default_search.php

<?php
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST,GET');
header('Access-Control-Allow-Credentials:true'); 
header("Content-Type: application/json;charset=utf-8"); 

error_reporting(E_ERROR); 
ini_set("display_errors","Off");  //屏蔽掉PHP警告和错误提示

if ($_SERVER["REQUEST_METHOD"] == "GET") {
    getMethod();
} elseif ($_SERVER["REQUEST_METHOD"] == "POST"){
    postMethod();
}

function getMethod(){    
    $result = "{\"total\":100,\"data\":[{\"id\":10001,\"name\":\"scott\"},{\"id\":10002,\"name\":\"tiger\"}]}";
    if($_GET["data"]){
        $result = '{"success":true,"defaultSearch":"'.$defaultSearch.'"}';
    }
    echo $result;//echo返回json格式化数据对{"success":true,"defaultSearch":"'.$defaultSearch.'"}
 } 

?>

通过echo返回json数据。

效果:

 

 参考博文:

https://blog.csdn.net/chuanyu/article/details/45920039

https://www.php.cn/php-ask-433923.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值