Home > Article > Web Front-end > Understanding the five common Ajax submission methods
To understand the five commonly used submission methods of AJAX, specific code examples are required
AJAX (Asynchronous JavaScript and XML) is a method used to create interactive web applications. technology. It allows partial page content to be updated through asynchronous communication with the server without refreshing the entire page. AJAX is widely used in modern web development to provide users with a better interactive experience.
In AJAX, data submission is a very important part. The following will introduce the five commonly used AJAX submission methods, as well as specific code examples for each method.
var xmlhttp = new XMLHttpRequest(); var url = "server.php?name=John&age=20"; xmlhttp.open("GET", url, true); xmlhttp.send();
var xmlhttp = new XMLHttpRequest(); var url = "server.php"; var params = "name=John&age=20"; xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // 请求成功后的处理逻辑 console.log(xmlhttp.responseText); } }; xmlhttp.send(params);
var xmlhttp = new XMLHttpRequest(); var url = "server.php"; var formData = new FormData(); formData.append("name", "John"); formData.append("age", "20"); xmlhttp.open("POST", url, true); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // 请求成功后的处理逻辑 console.log(xmlhttp.responseText); } }; xmlhttp.send(formData);
var xmlhttp = new XMLHttpRequest(); var url = "server.php"; var data = {name: "John", age: 20}; xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-type", "application/json"); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // 请求成功后的处理逻辑 console.log(xmlhttp.responseText); } }; xmlhttp.send(JSON.stringify(data));
var xmlhttp = new XMLHttpRequest(); var url = "server.php"; var xmlData = '<?xml version="1.0" encoding="UTF-8"?><data><name>John</name><age>20</age></data>'; xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-type", "text/xml"); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // 请求成功后的处理逻辑 console.log(xmlhttp.responseText); } }; xmlhttp.send(xmlData);
The above are specific code examples of the five commonly used AJAX submission methods. By understanding and practicing these submission methods, you can better use AJAX technology to process data and improve the user experience of your web applications.
The above is the detailed content of Understanding the five common Ajax submission methods. For more information, please follow other related articles on the PHP Chinese website!