Detailed explanation of Postman's FormData parameter passing usage

        In the first half of this year, because of the completion of the project, I came into contact with the backend by myself, and I also used postman to test the interface. After seeing the parameter form on the postman side, I have always had ideas about this formData.

        In the process of connecting the front-end and back-end interfaces after completion, I generally use the form of url splicing for get or delete requests, because according to the restAPI format, I basically use both to obtain detailed information about something, or to delete something The details of a thing, and as for the most frequently used post request, I generally use the json format syntax to pass parameters. In json, the front end only needs to pass the corresponding object, but I have always The parameter method is a bit misunderstood. It sounds like the name is passed as a parameter.
 

1. The way to create a formData object instance

1. Create an empty object

var formData = new FormData();//通过append方法添加数据
复制代码

2. Use an existing form to initialize the object

//表单示例
<form id="myForm" action="" method="post">
    <input type="text" name="name">名字
    <input type="password" name="psw">密码
    <input type="submit" value="提交">
</form>
//方法示例
// 获取页面已有的一个form表单
var form = document.getElementById("myForm");
// 用表单来初始化
var formData = new FormData(form);
// 我们可以根据name来访问表单中的字段
var name = formData.get("name"); // 获取名字
var psw = formData.get("psw"); // 获取密码
// 当然也可以在此基础上,添加其他数据
formData.append("token","kshdfiwi3rh");
复制代码

2. Operation method

The data stored in formData exists in the form of key-value pairs, the key is unique, and one key may correspond to multiple values.
If form initialization is used, each form field corresponds to a piece of data, their HTML name attribute is the key value, and their value attribute corresponds to the value value.
1. Get the value

//通过get(key)/getAll(key)来获取对应的value
formData.get("name"); // 获取key为name的第一个值
formData.get("name"); // 返回一个数组,获取key为name的所有值
复制代码

2 Add data

//通过append(key, value)来添加数据,如果指定的key不存在则会新增一条数据,如果key存在,则添加到数据的末尾
formData.append("k1", "v1");
formData.append("k1", "v2");
formData.append("k1", "v3");
复制代码

The method and result of obtaining the value are as follows:

formData.get("k1"); // "v1"
formData.getAll("k1"); // ["v1","v2","v3"]
复制代码

3. Set and modify data

//set(key, value)来设置修改数据,如果指定的key不存在则会新增一条,如果存在,则会修改对应的value值
formData.append("k1", "v1");
formData.set("k1", "1");
formData.getAll("k1"); // ["1"]
复制代码

4. Determine whether there is corresponding data

//has(key)来判断是否对应的key值
formData.append("k1", "v1");
formData.append("k2",null);
 
formData.has("k1"); // true
formData.has("k2"); // true
formData.has("k3"); // false
复制代码

5. Delete data

//delete(key)删除数据
formData.append("k1", "v1");
formData.append("k1", "v2");
formData.append("k1", "v1");
formData.delete("k1");
 
formData.getAll("k1"); // []
复制代码

3. JQuery example

//添加数据方式见上二。
//processData: false, contentType: false,多用来处理异步上传二进制文件。
 $.ajax({
    url: 'xxx',
    type: 'POST',
    data: formData,                    // 上传formdata封装的数据
    dataType: 'JSON',
    cache: false,                      // 不缓存
    processData: false,                // jQuery不要去处理发送的数据
    contentType: false,                // jQuery不要去设置Content-Type请求头
    success:function (data) {           //成功回调
        console.log(data);
    }
});
复制代码

·

/**
 * 将以base64的图片url数据转换为Blob文件格式
 * @param urlData 用url方式表示的base64图片
 */
function convertBase64UrlToBlob(urlData) {
    var bytes = window.atob(urlData.split(',')[1]); //去掉url的头,并转换为byte
    //处理异常,将ascii码小于0的转换为大于0
    var ab = new ArrayBuffer(bytes.length);
    var ia = new Uint8Array(ab);
    for(var i = 0; i < bytes.length; i++) {
        ia[i] = bytes.charCodeAt(i);
    }
    return new Blob([ab], {
        type: 'image/png'
    });
}
复制代码

The above is my understanding and use of FormData.

Guess you like

Origin blog.csdn.net/qq_53114797/article/details/130235528