jQuery-Ajax請(qǐng)求的三種方式

1. get方式

語(yǔ)法:
$.get(url, [data], [callback], [type])
參數(shù)說(shuō)明:
url 請(qǐng)求的地址
data 要發(fā)送給后臺(tái)的數(shù)據(jù): 要么是對(duì)象格式{key1:value1,key2:value2},要字符串拼接key1=value1&key2=value3
callback 回調(diào)函數(shù),返回服務(wù)器端的結(jié)果
type 返回的數(shù)據(jù)的格式,xml, json, text

1. get方式代碼

  <script src="jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
  <script type="text/javascript">
    //1. get方式     $.get(url, [data], [callback], [type])
    $.get("userInfo.json", function (userData) {
        console.log("get方式", userData);
    }, "json");
    </script>

2. post方式

語(yǔ)法:
$.post(url, [data], [callback], [type])
參數(shù)說(shuō)明:
url 請(qǐng)求的地址
data 要發(fā)送給后臺(tái)的數(shù)據(jù): 要么是對(duì)象格式{key1:value1,key2:value2},要字符串拼接key1=value1&key2=value3
callback 回調(diào)函數(shù),返回服務(wù)器端的結(jié)果
type 返回的數(shù)據(jù)的格式,xml, json, text

2. post方式代碼

<script src="jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    //2. post方式     $.post(url, [data], [callback], [type])
    $.post("userInfo.json", function (userData) {
        console.log("post方式", userData);
    }, "json");
  </script>

3. ajax方法,既可以支持get,也可以支持post

.ajax(配置對(duì)象) 語(yǔ)法:.ajax({
type: "POST", //發(fā)送的方式
url: "some.php", //請(qǐng)求的地址
data: "name=John&location=Boston", //要發(fā)送的數(shù)據(jù)
dataType: "返回的數(shù)據(jù)類型", //xml, json, text, jsonp【跨域請(qǐng)求】
success: function(msg){
//請(qǐng)求成功的回調(diào)函數(shù)
alert( "Data Saved: " + msg );
}
});

3. ajax方法代碼示例

<script src="jquery-1.11.0.js" type="text/javascript" charset="utf- 8"></script>
   <script type="text/javascript">
    //3. ajax通用方法
    $.ajax({
        type: "POST", //發(fā)送的方式
        url: "userInfo.json", //請(qǐng)求的地址
        data: "name=John&location=Boston", //要發(fā)送的數(shù)據(jù)
        dataType: "json", //xml, json, text, jsonp【跨域請(qǐng)求】
        success: function (userData) {
            //請(qǐng)求成功的回調(diào)函數(shù)
            console.log("ajax通用方法", userData);
        }
    });
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容