jQuery中使用Ajax

jQuery封好了许多直接可以使用的Ajax请求函数

GET请求

$.get()这个方法可以发起一个GET请求

// 琼台博客 www.qttc.net

$.get('/api_by_get', ($res) => {
  console.log($res);
})

在回调中获取响应结果

POST请求

$.post()这个方法可以发起一个POST请求

// 琼台博客 www.qttc.net

$.post'/api_by_post', "Hello world", ($res) => {
  console.log($res);
})

这个方法跟$.get()方法使用上极为相似,差别在于第二个参数为提交到服务器的数据

$.ajax()

$.ajax()是一个封装了很多配置项Ajax请求函数,使用例子

// 琼台博客 www.qttc.net

// Assign handlers immediately after making the request,
// and remember the jqXHR object for this request
var jqxhr = $.ajax( "example.php" )
  .done(function() {
    alert( "success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });
 
// Perform other work here ...
 
// Set another completion function for the request above
jqxhr.always(function() {
  alert( "second complete" );
});

更多具体的API使用说明可以看这个页面

http://api.jquery.com/jquery.ajax/

分享

TITLE: jQuery中使用Ajax

LINK: https://www.qttc.net/23-jquery-ajax.html

NOTE: 原创内容,转载请注明出自琼台博客