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格式
    积累知识,分享知识,学习知识。
  • 相关阅读:
    VirtualBox中的Linux读取Windows共享目录
    Windows10资源管理器去掉左侧“下载、文档、图片、音乐、视频”等目录
    在Eclipse ee中成功使用jQuery UI插件
    (medium)LeetCode .Implement Trie (Prefix Tree)
    (*medium)LeetCode 211.Add and Search Word
    (easy)LeetCode 257.Binary Tree Paths
    2016 360笔试 编程题 2
    2016 360笔试 编程题1
    (番外)使用DFS和BFS实现拓扑排序
    (medium)LeetCode 210.Course Schedule II
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/3940411.html
Copyright © 2011-2022 走看看