Post formdata via XMLHttpRequest object in JS ? ( cross browser)

后端 未结 2 1426
借酒劲吻你
借酒劲吻你 2020-12-30 16:27

Im trying to post a form data in js :

I have this code :

var formData = new FormData();
  formData.append(\"username\", \"Groucho\");
  formData.ap         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 16:46

    I wrote a simple wrapper that you can use to send FormData in IE (and it won't mess up anything in webkit/gecko either). Simply include the following js before you try to use FormData:

    var ieFormData = function ieFormData(){
    if(window.FormData == undefined)
    {
        this.processData = true;
        this.contentType = 'application/x-www-form-urlencoded';
        this.append = function(name, value) {
            this[name] = value == undefined ? "" : value;
            return true;
        }
    }
    else
    {
        var formdata = new FormData();
        formdata.processData = false;
        formdata.contentType = false;
        return formdata;
    }
    

    }

    Now simply switch all new FormData() calls to new ieFormData(), and switch

    processData: false, 
    contentType: false,
    

    to

    processData: formdata.processData,
    contentType: formdata.contentType,
    cache: false,
    

    and you're all set. Of course, this won't allow you to include files (you still need the iframe hack), but it will allow you to mimic FormData in IE.

提交回复
热议问题