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/n/333625.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
KBNVNKBNVN
上一篇 2025-02-01 13:34
下一篇 2025-02-01 13:34

相关推荐

  • 三星内存条参数用法介绍

    本文将详细解释三星内存条上面的各种参数,让你更好地了解内存条并选择适合自己的一款。 一、容量大小 容量大小是内存条最基本的参数,一般以GB为单位表示,常见的有2GB、4GB、8GB…

    编程 2025-04-29
  • Python3定义函数参数类型

    Python是一门动态类型语言,不需要在定义变量时显示的指定变量类型,但是Python3中提供了函数参数类型的声明功能,在函数定义时明确定义参数类型。在函数的形参后面加上冒号(:)…

    编程 2025-04-29
  • Python input参数变量用法介绍

    本文将从多个方面对Python input括号里参数变量进行阐述与详解,并提供相应的代码示例。 一、基本介绍 Python input()函数用于获取用户输入。当程序运行到inpu…

    编程 2025-04-29
  • Spring Boot中发GET请求参数的处理

    本文将详细介绍如何在Spring Boot中处理GET请求参数,并给出完整的代码示例。 一、Spring Boot的GET请求参数基础 在Spring Boot中,处理GET请求参…

    编程 2025-04-29
  • Hibernate日志打印sql参数

    本文将从多个方面介绍如何在Hibernate中打印SQL参数。Hibernate作为一种ORM框架,可以通过打印SQL参数方便开发者调试和优化Hibernate应用。 一、通过配置…

    编程 2025-04-29
  • Python Class括号中的参数用法介绍

    本文将对Python中类的括号中的参数进行详细解析,以帮助初学者熟悉和掌握类的创建以及参数设置。 一、Class的基本定义 在Python中,通过使用关键字class来定义类。类包…

    编程 2025-04-29
  • Python函数名称相同参数不同:多态

    Python是一门面向对象的编程语言,它强烈支持多态性 一、什么是多态多态是面向对象三大特性中的一种,它指的是:相同的函数名称可以有不同的实现方式。也就是说,不同的对象调用同名方法…

    编程 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

发表回复

登录后才能评论