检查formdata对象密钥是否存在

时间:2022-02-18 23:45:08

Is it possible to run a check on a key for the formdata object? I would like to know if a key has already been assigned a value.

是否可以对formdata对象的键进行检查?我想知道一个密钥是否已经分配了一个值。

tried something like this with negative results

尝试了这样的事情,结果不好

data=new FormData();
if(!data.key)
data.append(key,somevalue);

addition question is the nature of a double assignment to rewrite the original value?

另外一个问题是重写原始值的双重赋值的性质?

1 个解决方案

#1


3  

Things are changing and nowadays you can check if key exits using get function.

事情正在发生变化,现在你可以使用get函数检查密钥是否退出。

Original answer

原始答案

As we already discussed in comments, browser hides data which is stored in FormData object due to security reasons. There is one workaround which helps to preview its data in developer console which is described here: FormData.append("key", "value") is not working

正如我们在评论中已经讨论的那样,浏览器隐藏了由于安全原因而存储在FormData对象中的数据。有一种解决方法可以帮助在开发人员控制台中预览其数据,如下所述:FormData.append(“key”,“value”)不起作用

The only way to have access to such data in code is to use own wrapping object, which supports appending data, getting values and converting to FormData. It could be an object like this:

在代码中访问此类数据的唯一方法是使用自己的包装对象,该对象支持附加数据,获取值并转换为FormData。它可能是这样的对象:

function FormDataUnsafe() {
    this.dict = {};
};

FormDataUnsafe.prototype.append = function(key, value) {
    this.dict[key] = value;
};

FormDataUnsafe.prototype.contains = function(key) {
    return this.dict.hasOwnProperty(key);
};

FormDataUnsafe.prototype.getValue = function(key) {
    return this.dict[key];
};

FormDataUnsafe.prototype.valueOf = function() {
    var fd = new FormData();
    for(var key in this.dict) {
        if (this.dict.hasOwnProperty(key))
            fd.append(key, this.dict[key]);
    }

    return fd;
};

FormDataUnsafe.prototype.safe = function() {
    return this.valueOf();
};

Usage:

用法:

var xhr = new XMLHttpRequest;
xhr.open('POST', '/', true);
xhr.send(data.safe());  // convertion here

Demo

演示

#1


3  

Things are changing and nowadays you can check if key exits using get function.

事情正在发生变化,现在你可以使用get函数检查密钥是否退出。

Original answer

原始答案

As we already discussed in comments, browser hides data which is stored in FormData object due to security reasons. There is one workaround which helps to preview its data in developer console which is described here: FormData.append("key", "value") is not working

正如我们在评论中已经讨论的那样,浏览器隐藏了由于安全原因而存储在FormData对象中的数据。有一种解决方法可以帮助在开发人员控制台中预览其数据,如下所述:FormData.append(“key”,“value”)不起作用

The only way to have access to such data in code is to use own wrapping object, which supports appending data, getting values and converting to FormData. It could be an object like this:

在代码中访问此类数据的唯一方法是使用自己的包装对象,该对象支持附加数据,获取值并转换为FormData。它可能是这样的对象:

function FormDataUnsafe() {
    this.dict = {};
};

FormDataUnsafe.prototype.append = function(key, value) {
    this.dict[key] = value;
};

FormDataUnsafe.prototype.contains = function(key) {
    return this.dict.hasOwnProperty(key);
};

FormDataUnsafe.prototype.getValue = function(key) {
    return this.dict[key];
};

FormDataUnsafe.prototype.valueOf = function() {
    var fd = new FormData();
    for(var key in this.dict) {
        if (this.dict.hasOwnProperty(key))
            fd.append(key, this.dict[key]);
    }

    return fd;
};

FormDataUnsafe.prototype.safe = function() {
    return this.valueOf();
};

Usage:

用法:

var xhr = new XMLHttpRequest;
xhr.open('POST', '/', true);
xhr.send(data.safe());  // convertion here

Demo

演示