• 数据提交类型 Request Payload 与 Form Data 的区别总结


    大纲

    1.什么是`Request Payload`,什么是`Form Data`
    2.不同的`Content-Type`对应不同的提交方式
    3.总结
    
    • 1
    • 2
    • 3

    图形:
    在这里插入图片描述

    1.什么是Request Payload,什么是Form Data

    Request Payload (http请求体模式)
    Form Data (表单请求体模式)

    Request Payload 对应:

    Content-Type:application/json
    
    • 1

    Form Data 对应:

    Content-Type: application/x-www-form-urlencoded
    和
    Content-Type: multipart/form-data
    
    • 1
    • 2
    • 3

    备注:表单(POST请求)的编码



    类型含义
    application/x-www-form-urlencoded默认编码方式, 将数据编码成键值对形式
    multipart/form-data表单文件上传
    text/plain字符串型,数据获取方式 getInputStream

    2.不同的Content-Type对应不同的提交方式

    数据传输中一般类型只有3种
    1.Content-Type: text/plain
    2.Content-Type: application/x-www-form-urlencoded
    3.Content-Type: application/json

    • 传统ajax请求方式

      Content-Type:text/plain

    function submit2() {
        var xhr = new XMLHttpRequest();
        xhr.timeout = 3000;
        var obj = {a: 1, b: 2};
        xhr.open('POST', '/');
        xhr.send(obj);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    需要通过xhr.send(JSON.stringify(obj)) 修改为字符串后进行传输。

    Content-Type: text/plain
    Request Payload:  #字符串
    
    • 1
    • 2
    • axios方式请求

      Content-Type:application/json 和 Content-Type: application/x-www-form-urlencoded

    function submit3() {
        var sence1 = 'name=123&val=456';
        var sence2 = {name: 123, val: 456};
        axios.post('/', sence1)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # 请求数据为“字符”时:
    Content-Type: application/x-www-form-urlencoded
    Form Data:  #name=123&val=456
    
    # 请求数据为“对象”时:
    Content-Type:application/json
    Request Payload:  #{name:123,vla:456}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • Form表单提交
    <form action="/" method="POST">
        <input name="name" type="text">
        <input name="password" type="text">
        <button>提交</button>
    </form>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    Content-Type: application/x-www-form-urlencoded
    Form Data: # name='xxx'&password='yyy'
    
    • 1
    • 2

    3.总结

    Content-Type的差异

    模式类型
    ajaxContent-Type默认为 “文本” 类型
    form提交Content-Type默认为 “Form” 类型
    axios传递字符串Content-Type默认为 “Form” 类型
    axios传递对象Content-Type默认为 “JSON” 类型
  • 相关阅读:
    InnoDB存储引擎非常重要的一个机制--MVCC(多版本并发控制)
    css未知宽高,上下左右居中
    QT雷达扫描图
    LeetCode 414. Third Maximum Number【数组】简单
    华为云云耀云服务器L实例评测 | 实例评测使用之软件性能评测:华为云云耀云服务器下的 Redis 性能评测
    达梦数据库 优化
    建议收藏《Verilog代码规范笔记_华为》(附下载)
    生成本地的Docker镜像并上传到镜像仓库
    最长回文子序列 递归与动态规划
    kafka-- 安装kafka manager及简单使用
  • 原文地址:https://blog.csdn.net/weixin_35773751/article/details/125902284