zoukankan      html  css  js  c++  java
  • ajax返回数据类型为JSON数据的处理

    JSON数据处理:

    1.编码格式必须为utf8

    2.echo json_encode($db->GuanQuery($sql));   返回的是关联数组。json_encode返回的是json数据。

    3.大小写一定要区分,否则可能会查不到数据,报错。

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="jquery-1.11.2.min.js"></script>
    <title>无标题文档</title>
    </head>
    <body>
    <select id="nation"></select>
    </body>
    <script type="text/javascript">
    $.ajax({
        url:"jsonchuli.php",
        dataType:"JSON",
        success: function(data){
            var str ="";
                /*for(var i=0;i<data.length;i++)
                {
                    str = str+"<option value='"+data[i].Code+"'>"+data[i].Name+"</option>";
                }
                $("#nation").html(str);*/
                
                for(var s in data)//这种方法相当于foreach方法遍历,上面注释的方法是for循环。
                {
                    str = str+"<option value='"+data[s].Code+"'>"+data[s].Name+"</option>";
                }
                $("#nation").html(str);
            }
        })
    
    </script>

    php处理:

    <?php
    include("DBDA.class.php");
    $db = new DBDA();
    $sql = "select * from nation";
    
    //关联数组
    
    echo json_encode($db->GuanQuery($sql));
  • 相关阅读:
    why why why
    为什么又显示了呢?
    Hello
    兼容性问题
    前端性能优化
    ES6新增API
    详解面向对象、构造函数、原型与原型链
    post请求导出表单。
    vue在生产环境清除console.log
    js 延迟加载的几种方法
  • 原文地址:https://www.cnblogs.com/u1020641/p/6061217.html
Copyright © 2011-2022 走看看