ajax_异步交互-get/post方式
Ajax的異步交互:
客戶端向服務器端發(fā)送請求,直到服務器端進行響應,這個過程中,用戶可以做任何其他事情(不等).
實現Ajax的異步交互步驟(舉例說明):
get方式:
1.創(chuàng)建XMLHttpRequest核心對象
var xhr=getXhr();
2. 與服務器端建立連接
xhr.open("get","01.php?user=zhangwuji");
3. 客戶端向服務器端發(fā)送請求
//send()方法不起作用,但是不能被省略
xhr.send(null);
4. 客戶端接收服務器端的響應
??? xhr.onreadystatechange = function()
{ ???
if(xhr.readyState == 4 && xhr.status == 200)
{ ????
var data = xhr.responseText; ????
console.log(data); ???
}
?? }
? post方式
?1.創(chuàng)建XMLHttpRequest核心對象
2. 與服務器端建立連接
xhr.open("post","01.php");
3. 客戶端向服務器端發(fā)送請求
//send()方法起作用
?????//在send()方法被調用前,使用setRequestHeader()方法設置請求頭信息
? xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
?? xhr.send("user=zhangwuji");
4. 客戶端接收服務器端的響應
xhr.onreadystatechange = function()
{ ???
if(xhr.readyState == 4 && xhr.status == 200)
{
???? var data = xhr.responseText;
???? console.log(data);
??? }
?}
?
轉載于:https://www.cnblogs.com/yulingjia/p/4992787.html
總結
以上是生活随笔為你收集整理的ajax_异步交互-get/post方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WEB 服务器配置
- 下一篇: objective-c 中代码块(blo