jQuery中的异步请求是通过$.ajax方法来实现的。异步请求是指在发送请求的同时,可以继续执行其他的操作,在服务器端返回数据后再执行响应的操作。

$.ajax({url: "example.php",type: "POST",data: { name: "John", location: "Boston" },success: function (response) {console.log(response);},error: function (xhr, status, error) {console.log(error);}});

上面的代码是发送一个POST请求到example.php文件,并且传递了name和location两个参数。当服务器端返回数据后,会执行success回调函数。如果发生错误,则会执行error回调函数。

在异步请求中,常常使用的是JSON格式的数据。可以通过设置dataType属性来指定请求的数据格式。

$.ajax({url: "example.php",type: "GET",dataType: "json",success: function (response) {console.log(response);},error: function (xhr, status, error) {console.log(error);}});

上面的代码中,通过设置dataType为json来指定请求返回的数据格式为JSON。在success回调函数中处理返回的JSON数据。

在异步请求中,还可以通过cache属性来控制请求的缓存。如果设置为false,则禁用缓存。

$.ajax({url: "example.php",type: "GET",cache: false,success: function (response) {console.log(response);},error: function (xhr, status, error) {console.log(error);}});

上面的代码中,通过设置cache为false来禁用缓存。

jquery里的异步请求