jQuery 发送 AJAX 请求
AJAX?請求狀態
xhr.readyState 可以用來查看請求當前的狀態
參考官方文檔的鏈接:https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/readyState
0: 表示 XMLHttpRequest 實例已經生成,但是 open()方法還沒有被調用
1: 表示 send()方法還沒有被調用,仍然可以使用 setRequestHeader(),設定 HTTP請求的頭信息。
2: 表示 send()方法已經執行,并且頭信息和狀態碼已經收到。
3: 表示正在接收服務器傳來的 body 部分的數據。
4: 表示服務器數據已經完全接收,或者本次接收已經失敗了
jQuery 中的 AJAX
? ?get?請求
? ? $.get(url, [data], [callback], [type])
url:請求的 URL 地址。
data:請求攜帶的參數。
callback:載入成功時回調函數。
type:設置返回內容格式,xml, html, script, json, text, _default
? ?post?請求
? ? $.post(url, [data], [callback], [type])
url:請求的 URL 地址。
data:請求攜帶的參數。
callback:載入成功時回調函數。
type:設置返回內容格式,xml, html, script, json, text, _default。
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"><title>jQuery 發送 AJAX 請求</title><link crossorigin="anonymous" href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"><script crossorigin="anonymous" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body><div class="container"><h2 class="page-header">jQuery發送AJAX請求 </h2><button class="btn btn-primary">GET</button><button class="btn btn-danger">POST</button><button class="btn btn-info">通用型方法ajax</button></div><script>$('button').eq(0).click(function(){$.get('http://127.0.0.1:8000/jquery-server', {a:100, b:200}, function(data){console.log(data);},'json');});$('button').eq(1).click(function(){$.post('http://127.0.0.1:8000/jquery-server', {a:100, b:200}, function(data){console.log(data);});});$('button').eq(2).click(function(){$.ajax({//urlurl: 'http://127.0.0.1:8000/jquery-server',//參數data: {a:100, b:200},//請求類型type: 'GET',//響應體結果dataType: 'json',//成功的回調success: function(data){console.log(data);},//超時時間timeout: 2000,//失敗的回調error: function(){console.log('出錯啦!!');},//頭信息headers: {c:300,d:400}});});</script> </body> </html>
總結
以上是生活随笔為你收集整理的jQuery 发送 AJAX 请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: axios 发送 AJAX请求
- 下一篇: jQuery 对象和 DOM 对象