页面-功能-异步请求-ajax-
一、异步请求发起
几种方式:
第一种:
使用jquery
$.ajax({ url: "", type: "post", dataType: "json", data: $("#taskForm").serializeArray(),//获取表单的参数,转为json字符 contentType: "application/x-www-form-urlencoded", success: function (r) { alert(JSON.stringify(r)); } });
第二种:
使用JavaScript:
XMLHttpRequest 对象
var xhttp; if (window.XMLHttpRequest) { // 针对现代浏览器的代码 xhttp = new XMLHttpRequest(); } else { // 针对 IE6、IE5 的代码 xhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", "/demo/js/ajax_info.txt", true); xhttp.send();
二、发送数据json获取
第一种:自己拼接
第二种:从表单中获取
data: $("#taskForm").serializeArray(),
三、处理回传json对象