JQuery中$.ajax()方法详解jquery

/ / 2021-04-13   阅读:2498
JQuery中$.ajax()方法详解...
格式:
$.ajax({
    url: "http://www.hzhuti.com",    //请求的url地址
    dataType: "json",   //返回格式为json
    async: true, //请求是否异步,默认为异步,这也是ajax重要特性
    data: { "id": "value" },    //参数值
    type: "GET",   //请求方式
    beforeSend: function() {
        //请求前的处理
    },
    success: function(req) {
        //请求成功时处理
    },
    complete: function() {
        //请求完成的处理
    },
    error: function() {
        //请求出错处理
    }
});

参数列表:


实例:
$(function(){
     $('#send').click(function(){
          $.ajax({
              type: "GET",
              url: "test.json",
              data: {username:$("#username").val(), content:$("#content").val()},
              dataType: "json",
              success: function(data){
                          $('#resText').empty();   //清空resText里面的所有内容
                          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);
                       }
          });
     });
 });


我要评论

昵称:
验证码:

最新评论

共0条 共0页 10条/页 首页 上一页 下一页 尾页
意见反馈