0

I have this method that uses AJAX to post data from a form to a web method in the C# backend. Unfortunately, the method is not being hit. I did my research and all accessible resources states that these are the required AJAX options for passing form data to a web method.

Can somebody help me understand why this is not working?

Note: Application is asp.net webforms

<input type="file" id="uploader" />
var fileUpload = $("#uploader").get(0);
var files = fileUpload.files;
var fileData = new FormData();
for (var i = 0; i < files.length; i++) {
    fileData.append(files[i].name, files[i]);
    console.log(files[i]);
}


$.ajax({
    type: "POST",
    url: "Pagename.aspx/method",
    data: fileData,
    contentType: false,
    cache: false,
    dataType: 'json',
    processData: false,
})
Walters
  • 119
  • 16
  • 1
    What is the response from the server? – David Jan 18 '21 at 22:06
  • There are no error responses or responses at all. The method is not being hit and browser console doesn't contain an failure details @David – Walters Jan 18 '21 at 22:07
  • So the AJAX request just hangs indefinitely? Or is it not being requested in the first place? – David Jan 18 '21 at 22:09
  • Its just not posting to the web method at all. I have several ajax requests in this application but the only one that is not working is this one which contains contentType: false and processData: false. – Walters Jan 18 '21 at 22:13
  • 1
    Then what *is* it doing? In your browser’s debugging tools what is the request/response info for this in the network tab? If the request isn’t being made at all then it sounds like the problem is somewhere else. – David Jan 18 '21 at 22:15
  • Did you try to add a console.log() somewhere to confirm the ajax is being called? – Louys Patrice Bessette Jan 18 '21 at 22:15
  • ```` $.ajax({ url: 'Pagename.aspx/method', type: 'post', data: fileData, contentType: false, processData: false, success: function(response){ }, }); check here--- https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload – MD. RAKIB HASAN Jan 19 '21 at 06:46
  • Couple more things: should not be a comma at end of processData; should be a semicolon at the end of the ajax method; (should be a success callback). Is there a ScriptManager on the page? – wazz Jan 19 '21 at 14:45
  • I was playing around with this and got weird results until I added `contentType: "application/json; charset=utf-8"`. – wazz Jan 19 '21 at 14:59

1 Answers1

1

Here are the changes I made to at least get it to return a response. I did not work with the file upload at all. See if this gets it going.

Also note the use of JSON.stringify() on the 'data' line.

$.ajax({
    type: "POST",
    url: "test.aspx/method",
    data: JSON.stringify(fileData),
    contentType: "application/json; charset=utf-8",
    cache: false,
    dataType: 'json',
    processData: false
}).done(function(data) {
    console.log(data);
});

Web method:

[WebMethod]
public static string method()
{
    return "success";
}
wazz
  • 4,953
  • 5
  • 20
  • 34