返回值:XMLHttpRequest jQuery.get(url, [data] , [callback] , [type] )
概述
通過遠端 HTTP GET 請求載入資訊。
這是一個簡單的 GET 請求功能以取代複雜 $.ajax 。請求成功時可呼叫回撥函式。如果需要在出錯時執行函式,請使用 $.ajax。
jQuery 1.12 中 jQuery.post 和 jQuery.get 支援對像參數,這樣一來好處還比較多,比如設定回撥函式的context,或者跨域 post 時可以withCredential: true。用法可以參考最後一個示例。
參數
url,[data],[callback],[type] String,Map,Function,String V1.0
url :待載入頁面的URL地址
data :待發送 Key/value 參數。
callback :載入成功時回撥函式。
type :返回內容格式,xml, html, script, json, text, _default。
示例
描述:
請求 test.php 網頁,忽略返回值。
jQuery 程式碼:
$.get("test.php");
描述:
請求 test.php 網頁,傳送2個參數,忽略返回值。
jQuery 程式碼:
$.get("test.php", { name: "John", time: "2pm" } );
描述:
顯示 test.php 返回值(HTML 或 XML,取決於返回值)。
jQuery 程式碼:
$.get("test.php", function(data){
alert("Data Loaded: " + data);
});
描述:
顯示 test.cgi 返回值(HTML 或 XML,取決於返回值),新增一組請求參數。
jQuery 程式碼:
$.get("test.cgi", { name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
});
描述:
jQuery 1.12 中 jQuery.get()支援對像參數,具體的參數可以參考 $.ajax():
jQuery 程式碼:
jQuery.post({
url: 「/example」
});