FormData不是构造函数

I'm trying to make a ajax request to upload a image. My problem is when I create the FormData. My console is saying "dataForm is not a constructor".

How can I solve this ?

here is my script

$("#new-broadcast-image-static").on("change", function(formData) {
                var formData = new formData();

                // line that console point the error //
                var file = $("#new-broadcast-image-static")[0].files[0];
                formData.set("image", file);

                $.ajax({
                    url: apiUrl + "image/upload",
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function(data) {
                        hashNewBroadcastImage = data.data.identifier;
                        $("#hash-new-broadcast-image-static").val(hashNewBroadcastImage);
                    }
                });
            });

Capitalize it: var formData = new FormData();

But what are you trying to acomplish anyways? You are reasigning a variable you are getting as parameter:

 $("#new-broadcast-image-static").on("change", function(formData) {
      var formData = new formData();

You probably want to change it to something like

 $("#new-broadcast-image-static").on("change", function(e) {
      var formData = new FormData();

This code literally makes no sense:

// function will create a variable called formData
function(formData) {
  // Create a new variable?  And attempt to create an object with the
  // same name as a varaible?  WTF
  var formData = new formData();

Not totally sure about it, but I think you have a capital letter mistake, you've write formData() instead of FormData()

The correct way:

var formData = new FormData();

Please Check this Possibility it will definitely solve your problem


Anywhere in your JS Code if you are assigning a value to FormData like below

FormData={"PersonID":1,"PersonName":"Test"};

and if you use like this

var a = new FormData();

Then it will throw an error like "FormData is not a constructor as it is already declared as an Object".