• json


    一.通过现有的数据直接返回

    demo.html

    <script type="text/javascript">
       $(function(){
            $('#send').click(function() {
                 $.getJSON('test.json', function(data) {
                     $('#resText').empty();
          var html = '';
          $.each( data  , function(commentIndex, comment) {
           html += '<div class="comment"><h6>' + comment['username'] + ':</h6><p class="para">' + comment['content'] + '</p></div>';
          })
         $('#resText').html(html);
                })
           })
       })
    </script>//方式一

    <script type="text/javascript">
       $(function(){
            $('#send').click(function() {
                $.ajax({
         type: "GET",
         url: "test.json",
         dataType: "json",
         success : function(data){
                $('#resText').empty();
           var html = '';
           $.each( data  , function(commentIndex, comment) {
            html += '<div class="comment"><h6>' + comment['username'] + ':</h6><p class="para">' + comment['content'] + '</p></div>';
           })
          $('#resText').html(html);
         }
       });
            });
       })
    </script>//方式二

    <body>
     <br/>
     <p>
      <input type="button" id="send" value="加载"/>
     </p>
    <div  class="comment">已有评论:</div>
     <div id="resText" >
     </div>
    </body>

    test.json

    [
      {
        "username": "张三",
        "content": "沙发."
      },
      {
        "username": "李四",
        "content": "板凳."
      },
      {
        "username": "王五",
        "content": "地板."
      }
    ]

    二.后端从前端获得数据再返回前端

    <script type="text/javascript">
     $(function(){
        $("#send").click(function(){
       $.get("get3.php", {
          username :  $("#username").val() ,
          content :  $("#content").val() 
         }, function (data, textStatus){
             var username = data.username;
          var content = data.content;
             var txtHtml = "<div class='comment'><h6>"+username+":</h6><p class='para'>"+content+"</p></div>";
                            $("#resText").html(txtHtml); // 把返回的数据添加到页面上
         },"json");
         return false;
        })
     })
    </script>

    <body>
    <form id="form1" action="#">
    <p>评论:</p>
     <p>姓名: <input type="text" name="username" id="username" /></p>
     <p>内容: <textarea name="content" id="content"  rows="2" cols="20"></textarea></p>
     <p><input type="button" id="send" value="提交"/></p>
    </form>
    <div  class='comment'>已有评论:</div>
    <div id="resText" >
    </div>
    </body>

    get3.php

    <?php
    header("Content-Type:text/html; charset=utf-8");
    $arr = array('username'=>$_GET['username'],'content'=>$_GET['content']);
    echo json_encode($arr);
    ?>

  • 相关阅读:
    负margin在页面布局中的应用
    2018-05-04 圣杯布局 and 双飞翼布局,display:flex
    vue 动态加载图片路径报错解决方法
    vue 带参数的跳转-完成一个功能之后 之后需要深思,否则还会忘记
    vue项目打包后打开空白解决办法
    css 居中方法
    vue 不用npm下载安装包 该如何引用js
    安装WAMP 及 修改MYSQL用户名 、 密码
    Python 软件开发目录规范
    Python 1-3区分Python文件的两种用途和模块的搜索路径
  • 原文地址:https://www.cnblogs.com/pcd12321/p/4531489.html
Copyright © 2020-2023  润新知