首頁(yè)  >  ajax  > $.get(url,[data],[fn],[type])

返回值:XMLHttpRequestjQuery.get(url, [data], [callback], [type])

jQuery $.get() 方法 概述

通過(guò)遠(yuǎn)程 HTTP GET 請(qǐng)求載入信息。

這是一個(gè)簡(jiǎn)單的 GET 請(qǐng)求功能以取代復(fù)雜 $.ajax 。請(qǐng)求成功時(shí)可調(diào)用回調(diào)函數(shù)。如果需要在出錯(cuò)時(shí)執(zhí)行函數(shù),請(qǐng)使用 $.ajax。

jQuery 1.12 中 jQuery.post 和 jQuery.get 支持對(duì)象參數(shù),這樣一來(lái)好處還比較多,比如設(shè)置回調(diào)函數(shù)的context,或者跨域 post 時(shí)可以withCredential: true。用法可以參考最后一個(gè)示例。

參數(shù)

url,[data],[callback],[type]String,Map,Function,StringV1.0

url:待載入頁(yè)面的URL地址

data:待發(fā)送 Key/value 參數(shù)。

callback:載入成功時(shí)回調(diào)函數(shù)。

type:返回內(nèi)容格式,xml, html, script, json, text, _default。

示例

描述:

請(qǐng)求 test.php 網(wǎng)頁(yè),忽略返回值。

jQuery 代碼:
$.get("test.php");

描述:

請(qǐng)求 test.php 網(wǎng)頁(yè),傳送2個(gè)參數(shù),忽略返回值。

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,取決于返回值),添加一組請(qǐng)求參數(shù)。

jQuery 代碼:
$.get("test.cgi", { name: "John", time: "2pm" },
          function(data){
          alert("Data Loaded: " + data);
});

描述:

jQuery 1.12 中 jQuery.get()支持對(duì)象參數(shù),具體的參數(shù)可以參考 $.ajax():

jQuery 代碼:
jQuery.post({
            url: “/example”
});