[AJAX] jQuery for AJAX
1. $.ajax()
-
Definition and Usage
Ajax () method for performing AJAX (Asynchronous HTTP) request.All jQuery AJAX methods use ajax () method. The method generally used to request other methods can not be completed.
-
Syntax
$ .ajax ({name: value, name: value, ...})
This parameter specifies the name of one or more AJAX request / value pairs.
The following table lists the possible name / value:
name | Value / Description |
---|---|
async | Boolean value that indicates whether the request asynchronous processing. The default is true. |
beforeSend(xhr) | Function request before sending operation. |
cache | Boolean value that indicates whether the browser cache the requested page. The default is true. |
complete(xhr,status) | Function to run when the request is complete (after the request was successful or failed call, ie after success and error function). |
contentType | Transmitting data to the server when the content type is used. The default is: "application / x-www-form-urlencoded". |
context | All relevant provisions of AJAX callbacks "this" value. |
data | Provision of data to be sent to the server. |
dataFilter(data,type) | XMLHttpRequest function for processing the raw response data. |
dataType | Expected data type of the server response. |
error(xhr,status,error) | If the function fails to run the request. |
global | Boolean value that specifies whether the request to trigger global AJAX event handler. The default is true. |
ifModified | Whether the Boolean value that specifies only since the last request response changes when the request is successful. The default is false. |
jsonp | String in a callback function to rewrite the jsonp. |
jsonpCallback | It specifies the name of the callback function in a jsonp in. |
password | The provisions of the password used in the HTTP access authentication request. |
processData | Boolean value that specifies whether the data string is converted to a query request sent. The default is true. |
scriptCharset | Provisions requested character set. |
success(result,status,xhr) | When the function is run when the request is successful. |
timeout | Setting the local request time (in milliseconds). |
traditional | Boolean value that specifies whether to use a sequence of parameters of the traditional style. |
type | Type (GET or POST) in a predetermined request. |
url | URL specified request was sent. The default is the current page. |
username | Require the use of user names in HTTP access authentication request. |
xhr | A function to create XMLHttpRequest object. |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery实现ajax</title>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
//定义方法
function fun() {
//使用$.ajax()发送异步请求
$.ajax({
url:"ajaxServlet",//请求路径
type:"POST",//请求方式
//data:"username=jack&age=23",//请求参数
data:{"username":"jack","age":23},
success:function (data) {
alert(data);
},//响应成功后的回调函数
error:function(){
alert("出错了")
},//表示如果请求响应出现错误,会执行的回调函数
dataType:"text"//设置接收到的响应数据的格式
});
}
</script>
</head>
<body>
<input type="button" value="发送异步请求" onclick="fun()">
<input>
</body>
</html>
2. $.get()
Loading information through remote HTTP GET request.
This is a simple GET request to replace the function of the complex $ .ajax. The callback function can be invoked when the request is successful. If you need to execute the function when an error occurs, use $ .ajax.
$.get(url,[data],[callback],[type]);
url:待载入页面的URL地址
data:待发送 Key/value 参数。
callback:载入成功时回调函数。
type:返回内容格式,xml, html, script, json, text, _default。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery实现ajax</title>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
//定义方法
function fun() {
//使用$.get()发送异步请求
$.get("ajaxServlet",{username:"rose"},function (data) {
alert(data);
},"text");
}
</script>
</head>
<body>
<input type="button" value="发送异步请求" onclick="fun()">
<input>
</body>
</html>
3. $.post()
Loading information requested by remote HTTP POST.
This is a simple function of the POST request to replace the complex $ .ajax. The callback function can be invoked when the request is successful. If you need to execute the function when an error occurs, use $ .ajax.
$.post(url,[data],[callback],[type]);
url:发送请求地址。
data:待发送 Key/value 参数。
callback:发送成功时回调函数。
type:返回内容格式,xml, html, script, json, text, _default。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery实现ajax</title>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
//定义方法
function fun() {
//使用$.get()发送异步请求
$.post("ajaxServlet",{username:"rose"},function (data) {
alert(data)
},"text");
}
</script>
</head>
<body>
<input type="button" value="发送异步请求" onclick="fun()">
<input>
</body>
</html>