Ajax原生XmlHttpRequest的使用html
1. XmlHttpRequest对象的主要方法ajax
1 a. void open(String method,String url,Boolen async) 2 用于建立请求 3 4 参数: 5 method: 请求方式(字符串类型),如:POST、GET、DELETE... 6 url: 要请求的地址(字符串类型) 7 async: 是否异步(布尔类型) 8 9 b. void send(String body) 10 用于发送请求 11 12 参数: 13 body: 要发送的数据(字符串类型) 14 15 c. void setRequestHeader(String header,String value) 16 用于设置请求头 17 18 参数: 19 header: 请求头的key(字符串类型) 20 vlaue: 请求头的value(字符串类型) 21 22 d. String getAllResponseHeaders() 23 获取全部响应头 24 25 返回值: 26 响应头数据(字符串类型) 27 28 e. String getResponseHeader(String header) 29 获取响应头中指定header的值 30 31 参数: 32 header: 响应头的key(字符串类型) 33 34 返回值: 35 响应头中指定的header对应的值 36 37 f. void abort() 38 39 终止请求
2. XmlHttpRequest对象的主要属性json
1 a. Number readyState 2 状态值(整数) 3 4 详细: 5 0-未初始化,还没有调用open()方法; 6 1-启动,调用了open()方法,未调用send()方法; 7 2-发送,已经调用了send()方法,未接收到响应; 8 3-接收,已经接收到部分响应数据; 9 4-完成,已经接收到所有响应数据; 10 11 b. Function onreadystatechange 12 当readyState的值改变时自动触发执行其对应的函数(回调函数) 13 14 c. String responseText 15 服务器返回的数据(字符串类型) 16 17 d. XmlDocument responseXML 18 服务器返回的数据(Xml对象) 19 20 e. Number states 21 状态码(整数),如:200、404... 22 23 f. String statesText 24 状态文本(字符串),如:OK、NotFound...
3. XmlHttpRequest对象的完整使用代码服务器
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 9 <input type="button" value="AjaxGet" onclick="AjaxClickGet();"> 10 <input type="button" value="AjaxPost" onclick="AjaxClickPost();"> 11 12 <script> 13 function AjaxClickGet() { 14 var xhr = new XMLHttpRequest(); 15 xhr.open("GET", "/ajax/ajax_get.html/", true); 16 xhr.setRequestHeader("key1", "value1"); //设置请求头 17 xhr.send("name=root;pwd=123"); //注意send函数参数的固定格式 18 xhr.onreadystatechange = function (ev) { 19 if(xhr.readyState == 4){ 20 //接受数据完毕 21 console.log(xhr.responseText); 22 obj = JSON.parse(xhr.responseText); //字符串转为json对象 23 console.log(obj) 24 } 25 } 26 } 27 28 function AjaxClickPost() { 29 var xhr = new XMLHttpRequest(); 30 xhr.open("POST", "/ajax/ajax_post.html/", true); 31 //以 POST 方式发出请求时, 必须包含下面的 "请求头" 32 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset-UTF-8"); 33 xhr.send("name=to2bage;pwd=123"); 34 xhr.onreadystatechange = function (ev) { 35 if(xhr.readyState == 4){ 36 obj = JSON.parse(xhr.responseText); 37 console.log(obj) 38 } 39 } 40 } 41 </script> 42 </body> 43 </html>
jQuery的使用app
1. jQuery 的Ajax的方法异步
1 jQuery.get(...) 2 全部参数: 3 url: 待载入页面的URL地址 4 data: 待发送 Key/value 参数。 5 success: 载入成功时回调函数。 6 dataType: 返回内容格式,xml, json, script, text, html 7 8 9 jQuery.post(...) 10 全部参数: 11 url: 待载入页面的URL地址 12 data: 待发送 Key/value 参数 13 success: 载入成功时回调函数 14 dataType: 返回内容格式,xml, json, script, text, html 15 16 17 jQuery.getJSON(...) 18 全部参数: 19 url: 待载入页面的URL地址 20 data: 待发送 Key/value 参数。 21 success: 载入成功时回调函数。 22 23 24 jQuery.getScript(...) 25 全部参数: 26 url: 待载入页面的URL地址 27 data: 待发送 Key/value 参数。 28 success: 载入成功时回调函数。 29 30 31 jQuery.ajax(...) 32 33 部分参数: 34 35 url:请求地址 36 type:请求方式,GET、POST(1.9.0以后用method) 37 headers:请求头 38 data:要发送的数据 39 contentType:即将发送信息至服务器的内容编码类型(默认: "application/x-www-form-urlencoded; charset=UTF-8") 40 async:是否异步 41 timeout:设置请求超时时间(毫秒) 42 43 beforeSend:发送请求前执行的函数(全局) 44 complete:完成以后执行的回调函数(全局) 45 success:成功以后执行的回调函数(全局) 46 error:失败以后执行的回调函数(全局) 47 48 49 accepts:经过请求头发送给服务器,告诉服务器当前客户端课接受的数据类型 50 dataType:将服务器端返回的数据转换成指定类型 51 "xml": 将服务器端返回的内容转换成xml格式 52 "text": 将服务器端返回的内容转换成普通文本格式 53 "html": 将服务器端返回的内容转换成普通文本格式,在插入DOM中时,若是包含JavaScript标签,则会尝试去执行。 54 "script": 尝试将返回值看成JavaScript去执行,而后再将服务器端返回的内容转换成普通文本格式 55 "json": 将服务器端返回的内容转换成相应的JavaScript对象 56 "jsonp": JSONP 格式 57 使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数 58 59 若是不指定,jQuery 将自动根据HTTP包MIME信息返回相应类型(an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string 60 61 converters: 转换器,将服务器端的内容根据指定的dataType转换类型,并传值给success回调函数 62 $.ajax({ 63 accepts: { 64 mycustomtype: 'application/x-some-custom-type' 65 }, 66 67 // Expect a `mycustomtype` back from server 68 dataType: 'mycustomtype' 69 70 // Instructions for how to deserialize a `mycustomtype` 71 converters: { 72 'text mycustomtype': function(result) { 73 // Do Stuff 74 return newresult; 75 } 76 }, 77 }); 78 79 jQuery Ajax 方法列表