5

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.

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?

2
  • See this question: stackoverflow.com/questions/7752188/… Commented Feb 19, 2014 at 9:48
  • @tony Ok i can appreciate what the browser platform is trying to do with security I have an idea of what would be a suitable work around based off of your link. Why not why not give an example of putting it in a wrapper object that records the kv pairs and I'll accept it as an answer for reference Commented Feb 19, 2014 at 16:41

1 Answer 1

3

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

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

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:

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

Sign up to request clarification or add additional context in comments.

5 Comments

for a little more insight for those in the same situation. the recommended solution is to record what you put in in some form or other(answer does that in the form of a wrapping object). and use that reference to say what you already put in hence if the key already exists
At least, can I check if formdata object has a file inside?
Could you give some details about "browser hides data which is stored in FormData object due to security reasons." even if a link is preferred.
@PageYe this answer is very old and many things changed since that time, so it seems now it is not a security issue anymore.
@Tony I just want to know the reason why should browser hides data instead of showing the properties directly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.