• AJAX获取JSON形式的数据


    test.html:

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6     <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
     7     <script>
     8         $(function(){
     9             var url = 'test.php';
    10             $(document).on('click','button',function(){
    11                 var params = $('input').serialize();
    12                 //alert(params);        //user=test&pwd=123&sex=%E7%94%B7
    13                 $.ajax({
    14                     url : url,
    15                     type : 'get',
    16                     data : params,
    17                     dataType : 'json',
    18                     success : function(msg){
    19                         alert(msg.user+"
    "+msg.pwd+"
    "+msg.sex);
    20                     }
    21                 });
    22 
    23             });    
    24         });
    25     </script>
    26 </head>
    27 <body>
    28     用户名:<input type="text" name="user"><br>
    29     密 码:<input type="password" name="pwd"><br>
    30     性 别:<input type="radio" name="sex" value="男">31             <input type="radio" name="sex" value="女">32     <br>
    33     <button>提交</button>
    34 </body>
    35 </html>

     test.php:

    1 <?php 
    2     echo json_encode($_GET); //对$_GET数组进行 JSON 编码 结果:{"user":"admin","pwd":"123","sex":"u5973"}
    3 ?>

     结果:

    注意:input中的name属性是必须的!

  • 相关阅读:
    字符串方法
    函数的属性和方法
    数组的去重!!
    常见的数组方法
    JS中的函数
    JavaScript 中表达式和语句的区别
    运算符优先级
    题解 CF813B 【The Golden Age】
    题解 CF834B 【The Festive Evening】
    题解 CF810B 【Summer sell-off】
  • 原文地址:https://www.cnblogs.com/cloak/p/4978050.html
Copyright © 2020-2023  润新知