ajax的四种请求方式load()、 $.get() 、$.post() 、 $.ajax()

一、load()   

很少使用

对象.load(url,data,function (responseText, status, xhr){})

方法从服务器加载数据,并把返回的数据放入被选元素中。只能用于get请求

 url:地址

 data:传递数据

 responseText:服务端返回的数据

 status:状态    success/error

  xhr:ajax对象

二、 $.get()

一般不用

   $.get()  :只能用于get方法请求

   $.get(url,params,function(data,status){})

   data:返回的数据   status:状态

<body>
  <h1>在输入框中尝试输入字母 a:</h1>
  <div>
    <label for="">请输入:</label>
    <input type="text" id="text"/>
  </div>
  <p><span>提示信息:</span><span id="info"></span></p>
</body>

  
$('#text').keyup(function(){
    $.get('getHead.php?q=' + $('#text').val(),function(data,status){
      if(status=='success'){
        // console.log(data);
        $("#info").text(data)
      }
    })
  })

三、$.post()

一般不用

  $.post()  :只能用于post方法请求

  $.post(url,params,function(data,status){})

  data:返回的数据   status:状态

<body>
  <form action="" method="get">
    <div>
      <label for="">姓名:</label>
      <input type="text" name="" id="user" />
    </div>
    <div>
      <label for="">密码:</label>
      <input type="password" name="" id="pwd" />
    </div>
    <input type="button" value="登录提交" id="login"/>
  </form>
  <div id="box"></div>
</body>


 $('#login').bind('click',function(){
        
$.post('tt_post.php','user='+$('#user').val()+'&pwd='+$("#pwd").val(),function(data,status){
      if(status=='success'){
        console.log(data);
        var data=JSON.parse(data)
        // $("#box").html('<p>姓名:'+data.user+'</p><p>密码:'+data.pwd+'</p>')
        $("#box").append('<p>姓名:'+data.user+'</p><p>密码:'+data.pwd+'</p>')

      }
    })
  })

四、$.ajax()

使用最多,基本上都使用这个方法

 $('#text').keyup(function(){
    $.ajax({
      url: 'getHead.ph?x=' + $('#text').val(), //地址
      type:'GET',//请求方式
      async:true,//是否异步
      data:"",//向后端传递的数据(body传参)
      // dataType:'text',//预计服务端返回的数据格式
      success:function(res){//请求成功后的回调函数
        console.log(res);
      },
      error:function(err){//请求失败后的回调函数
        console.log(err);
      },
      complete:function(data){//请求成功or失败后的回调函数
        console.log(data);
      } 
    })
  })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值