Ajax參數詳解

一、url

url是發送請求的地址,可以是相對地址或者絕對地址,也可以是一個函數,函數返回值為發送請求的地址。

    $.ajax({
        url:"/api/users",    //相對地址
        method:"GET",
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })
    $.ajax({
        url:"http://www.example.com/api/users",    //絕對地址
        method:"GET",
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })
    $.ajax({
        url:function(){     //函數返回值為發送請求的地址
            return "/api/users?id="+$("#userId").val();
        },
        method:"GET",
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })

二、method

method指定HTTP請求方法,包括GET、POST、PUT、DELETE等。

    $.ajax({
        url:"/api/users",
        method:"GET",      //指定GET方法
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })
    $.ajax({
        url:"/api/users",
        method:"POST",     //指定POST方法
        data:{
            "name":"John",
            "age":"18"
        },
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })

三、data

data用來指定發送給伺服器的數據,可以是JSON格式的數據、XML格式的數據、FormData對象。

    $.ajax({
        url:"/api/users",
        method:"POST",
        data:{
            "name":"John",
            "age":"18"
        },                  //發送JSON格式的數據
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })
    $.ajax({
        url:"/api/users",
        method:"POST",
        data:$("#userData").serialize(),    //發送表單數據
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })
    var formData = new FormData();
    formData.append("file", $("#fileUpload")[0].files[0]);
    
    $.ajax({
        url:"/api/upload",
        method:"POST",
        data:formData,      //發送FormData對象
        contentType:false,
        processData:false,
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })

四、headers

headers用來指定HTTP請求頭。

    $.ajax({
        url:"/api/users",
        method:"GET",
        headers:{
            "Authorization":"Bearer "+localStorage.getItem("accessToken")    //授權頭
        },
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })
    $.ajax({
        url:"/api/users",
        method:"POST",
        headers:{
            "Content-Type":"application/json"   //指定請求體類型
        },
        data:JSON.stringify({
            "name":"John",
            "age":"18"
        }),
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })

五、dataType

dataType用來指定伺服器返回的數據類型,可以是”json”、”xml”、”html”、”text”。

    $.ajax({
        url:"/api/users",
        method:"GET",
        dataType:"json",        //指定返回JSON格式的數據
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })
    $.ajax({
        url:"/api/users",
        method:"GET",
        dataType:"xml",         //指定返回XML格式的數據
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })

六、async

async用來指定請求是否為非同步,默認為true。

    $.ajax({
        url:"/api/users",
        method:"GET",
        async:false,        //同步請求
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })

七、cache

cache用來指定是否允許瀏覽器緩存請求結果,默認為true。

    $.ajax({
        url:"/api/users",
        method:"GET",
        cache:false,        //不允許緩存
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })

八、timeout

timeout用來指定請求超時時間。

    $.ajax({
        url:"/api/users",
        method:"GET",
        timeout:5000,       //設置超時時間為5秒
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })

九、complete

complete用來指定請求完成後的回調函數。

    $.ajax({
        url:"/api/users",
        method:"GET",
        complete:function(xhr,status){     //請求完成後的回調函數
            console.log(xhr);
            console.log(status);
        }
    })

十、global

global用來指定是否開啟全局ajax事件,默認為true。

    $.ajax({
        url:"/api/users",
        method:"GET",
        global:false,      //關閉全局ajax事件
        success:function(res){
            console.log(res);
        },
        error:function(err){
            console.log(err);
        }
    })

原創文章,作者:KBNVN,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/333625.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
KBNVN的頭像KBNVN
上一篇 2025-02-01 13:34
下一篇 2025-02-01 13:34

相關推薦

  • 三星內存條參數用法介紹

    本文將詳細解釋三星內存條上面的各種參數,讓你更好地了解內存條並選擇適合自己的一款。 一、容量大小 容量大小是內存條最基本的參數,一般以GB為單位表示,常見的有2GB、4GB、8GB…

    編程 2025-04-29
  • Python3定義函數參數類型

    Python是一門動態類型語言,不需要在定義變數時顯示的指定變數類型,但是Python3中提供了函數參數類型的聲明功能,在函數定義時明確定義參數類型。在函數的形參後面加上冒號(:)…

    編程 2025-04-29
  • Spring Boot中發GET請求參數的處理

    本文將詳細介紹如何在Spring Boot中處理GET請求參數,並給出完整的代碼示例。 一、Spring Boot的GET請求參數基礎 在Spring Boot中,處理GET請求參…

    編程 2025-04-29
  • Python input參數變數用法介紹

    本文將從多個方面對Python input括弧里參數變數進行闡述與詳解,並提供相應的代碼示例。 一、基本介紹 Python input()函數用於獲取用戶輸入。當程序運行到inpu…

    編程 2025-04-29
  • Hibernate日誌列印sql參數

    本文將從多個方面介紹如何在Hibernate中列印SQL參數。Hibernate作為一種ORM框架,可以通過列印SQL參數方便開發者調試和優化Hibernate應用。 一、通過配置…

    編程 2025-04-29
  • Python函數名稱相同參數不同:多態

    Python是一門面向對象的編程語言,它強烈支持多態性 一、什麼是多態多態是面向對象三大特性中的一種,它指的是:相同的函數名稱可以有不同的實現方式。也就是說,不同的對象調用同名方法…

    編程 2025-04-29
  • Python Class括弧中的參數用法介紹

    本文將對Python中類的括弧中的參數進行詳細解析,以幫助初學者熟悉和掌握類的創建以及參數設置。 一、Class的基本定義 在Python中,通過使用關鍵字class來定義類。類包…

    編程 2025-04-29
  • 全能編程開發工程師必知——DTD、XML、XSD以及DTD參數實體

    本文將從大體介紹DTD、XML以及XSD三大知識點,同時深入探究DTD參數實體的作用及實際應用場景。 一、DTD介紹 DTD是文檔類型定義(Document Type Defini…

    編程 2025-04-29
  • Python可變參數

    本文旨在對Python中可變參數進行詳細的探究和講解,包括可變參數的概念、實現方式、使用場景等多個方面,希望能夠對Python開發者有所幫助。 一、可變參數的概念 可變參數是指函數…

    編程 2025-04-29
  • XGBoost n_estimator參數調節

    XGBoost 是 處理結構化數據常用的機器學習框架之一,其中的 n_estimator 參數決定著模型的複雜度和訓練速度,這篇文章將從多個方面詳細闡述 n_estimator 參…

    編程 2025-04-28

發表回復

登錄後才能評論