当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - Uncaught TypeError: Failed to execute 'append' on 'FormData': 2 arguments required, but only 1 present

问题描述:

I am sending the form data to web api controller in asp.net mvc but my ajax request not hitting the controller every time i got the above error kindly tell me how to send request with files in ajax in asp.net mvc to web api controller

$(document).on("submit", ".SignupForm", function (event) {

event.preventDefault();

var formData = new FormData($(".SignupForm"));

formData.append($('#imageFile')[0].files[0]);

$.ajax({

url: 'api/Countries',

type:'POST',

data: obj,

dataType: 'json',

processData: false,

success: function (result) {

console.log(result);

},

error: function (data, status, abc) {

console.log(data);

console.log(status);

console.log(abc);

}

});

});

网友答案:

See this: (apparently you must add argument)

SyntaxEDIT There are two versions of this method: a two and a three parameter version:

formData.append(name, value); formData.append(name, value, filename); Parameters

name The name of the field whose data is contained in value. value The field's value. This can be a USVString or Blob (including subclasses such as File). filename Optional The filename reported to the server (a USVString), when a Blob or File is passed as the second parameter. The default filename for Blob objects is "blob". The default filename for File objects is the file's filename. ExampleEDIT The following line creates an empty FormData object:

var formData = new FormData(); // Currently empty You can add key/value pairs to this using FormData.append:

formData.append('username', 'Chris'); formData.append('userpic', myFileInput.files[0], 'chris.jpg'); As with regular form data, you can append multiple values with the same name. For example (and being compatible with PHP's naming conventions by adding [] to the name):

formData.append('userpic[]', myFileInput1.files[0], 'chris1.jpg'); formData.append('userpic[]', myFileInput2.files[0], 'chris2.jpg'); This technique makes it simpler to process multi-file uploads because the resultant data structure is more conducive to looping.

https://developer.mozilla.org/en-US/docs/Web/API/FormData/append

分享给朋友:
您可能感兴趣的文章:
随机阅读: