Html从php获取json并解析

我会带着你远行 2022-09-10 01:17 437阅读 0赞

watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBAYW5keWxhdXJlbg_size_16_color_FFFFFF_t_70_g_se_x_16

先上html代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0-rc1.js"></script>
  5. <script type="text/javascript" src="http://code.jquery.com/jquery-3.4.1.min.js"></script>
  6. </head>
  7. <body>
  8. <div id="placeholder"></div>
  9. <script type="text/javascript">
  10. var jsonStr = "{\"total\":100,\"data\":[{\"id\":10001,\"name\":\"scott\"},{\"id\":10002,\"name\":\"tiger\"}]}";
  11. $(document).ready(function(){
  12. $.ajax({
  13. url:"http://localhost/default_search.php",
  14. type: "GET",
  15. dataType: "text",
  16. success: function(json){
  17. alert(json);//$a的值
  18. var jsonObj = window.JSON.parse(json);
  19. var str = "json字符串解析为json对象<br>";
  20. str += "<span>Total:"+jsonObj.total+"</span><br><span>Data:";
  21. for (var i=0;i<jsonObj.data.length ; i++)
  22. {
  23. str += "id:" + jsonObj.data[i].id + ",name:" + jsonObj.data[i].name+"<br>";
  24. }
  25. str += "</span><br>";
  26. document.querySelector("#placeholder").innerHTML = str;
  27. }
  28. })
  29. })
  30. </script>
  31. </body>
  32. </html>

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

  1. $(document).ready(function(){
  2. $.ajax({
  3. url:"http://localhost/default_search.php",
  4. type: "GET",
  5. dataType: "text",
  6. success: function(json){
  7. alert(json);//$a的值
  8. }
  9. })
  10. })

解析json

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

解析的数据显示在网页

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

php文件default_search.php

  1. <?php
  2. header('Access-Control-Allow-Origin:*');
  3. header('Access-Control-Allow-Methods:POST,GET');
  4. header('Access-Control-Allow-Credentials:true');
  5. header("Content-Type: application/json;charset=utf-8");
  6. error_reporting(E_ERROR);
  7. ini_set("display_errors","Off"); //屏蔽掉PHP警告和错误提示
  8. if ($_SERVER["REQUEST_METHOD"] == "GET") {
  9. getMethod();
  10. } elseif ($_SERVER["REQUEST_METHOD"] == "POST"){
  11. postMethod();
  12. }
  13. function getMethod(){
  14. $result = "{\"total\":100,\"data\":[{\"id\":10001,\"name\":\"scott\"},{\"id\":10002,\"name\":\"tiger\"}]}";
  15. if($_GET["data"]){
  16. $result = '{"success":true,"defaultSearch":"'.$defaultSearch.'"}';
  17. }
  18. echo $result;//echo返回json格式化数据对{"success":true,"defaultSearch":"'.$defaultSearch.'"}
  19. }
  20. ?>

通过echo返回json数据。

效果:

watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBAYW5keWxhdXJlbg_size_15_color_FFFFFF_t_70_g_se_x_16

20210903093633109.png

参考博文:

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

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

发表评论

表情:
评论列表 (有 0 条评论,437人围观)

还没有评论,来说两句吧...

相关阅读

    相关 unity请求json数据

    unity3d在跟.net进行http通信的时候,最常见的就是表单数据的提交请求了,但服务器端会返回一坨json数据,这就要求我们在unity中进行json数据的处理了,一般u

    相关 JSON

        [ JSON 使用讲解 ][JSON _]这篇文章讲解了,JSON的介绍以及使用GSON解析。今天,我们就在Android项目中使用两种方式解析JSON数据。如果你对J