zoukankan      html  css  js  c++  java
  • ajax 获取JSON

     4 html 代码
     5 
     6 <!doctype html>
     7 <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
     8 <html>
     9 <head>
    10     <meta charset="utf-8">
    11     <title>document</title>
    12     <link href="../css/app.css" type="text/css" rel="stylesheet" />
    13     <script src="../js/jquery-1.9.1.min.js" type="text/javascript"></script>
    14 </head>
    15 <script>
    16 
    17     $(function(){
    18 
    19         $("#getjson").click(function(){
    20             $.getJSON("/index/getJson", { key: "getjson"}, function(json){
    21                 alert("JSON Data: " + json.a);
    22             });
    23         })
    24         $(".json").click(function(){
    25             //用传统的ajax 注意其实上面的getJson 就是这种传统ajax一个简写而已
    26             $.ajax({
    27                 cache: true,
    28                 type: "POST",
    29                 url: '/index/getJson',
    30                 //data:$('#comform').serialize(),
    31                 async: false,
    32                 error: function(request) {
    33                     tipslog(" error!", 'tiperror', 2);
    34                 },
    35                 success: function(data) {
    36                     /*$obj = $.parseJSON(data);
    37                     alert($obj.a)  测试可用*/
    38                     $obj = eval("("+data+")");//通过测试可用 这是为了转成
    39                     alert($obj.a);
    40                 }
    41             });
    42 
    43         })
    44     })
    45 </script>
    46 <body>
    47 <h1>操他妈装B的人</h1>
    48 <input type="button" id = "getjson" value = "getJson">
    49 <input type = "button" id = "parsejson" class = "json" value ="parseJson" >
    50 <input type = "button" id = "eval" class = "json" value = "eval">
    51 </body>
    52 </html>
    53 
    54 后台代码
    55 public function getJsonAction(){
    56          $arr_test_json = array('a' => "abcd",'b' => "bcad",'c' => "cdas",'d' => "defg");
    57          $json = json_encode($arr_test_json);
    58          echo $json;
    59 }
    60 
    61 上述代码为PHP代码,java的话也有对应的函数将数组转json 也可以直接拼一个json格式
    积累知识,分享知识,学习知识。
  • 相关阅读:
    共享
    mac下搭建基于vue-cli 3.0的Element UI 项目
    rsync | scp文件同步命令使用
    在centos 6.9下Protocol Buffers数据传输及存储协议的使用(python)
    mysql中group by存在局限性探讨(待续)
    Protocol Buffers数据传输及存储协议简单使用
    简单数据库分表的思路
    mysql索引优化
    黑苹果相关资源
    JS 如何获取当前上一个月、下一个月和月份所含天数
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/3940411.html
Copyright © 2011-2022 走看看