0

I am trying to submit a form with FormData. But the payload is null.

I have tried creating the FormData object with the form as input and also by appending the form items to FormData

var claim = new FormData();
claim.append('DistributorPONr', $('#claim-form #distributor-po').val());
claim.append('SalesOrderNr', $('#claim-form #sales-order').val());

ajaxCall({
    url: '@Url.Action("ValidateForm", "Claims")',
    data: claim,  
    type: 'POST',
    cache: false,
    processData: false,
    contentType: false,
})


The payload for claim is empty in network

Andra Avram
  • 75
  • 1
  • 8
  • FYI, `$('#claim-form #distributor-po')` and `$('#distributor-po')` will always select the same element, since IDs must be unique to the document. The latter is marginally faster as well. – Heretic Monkey Oct 29 '19 at 20:36
  • I would delete the processData and contentType lines, since they are only needed for uploading files. I would add 'dataType: "JSON"', though. Furthermore I would finish the Ajax call and write the success and error functions, then Console.Log them to see if your server is responding with any type of error. – Brandon Miller Oct 29 '19 at 20:40
  • i am also trying to upload files however i just showed two attributes here for an example – Andra Avram Oct 29 '19 at 20:44
  • If you want to start with an actual form's data, you need to pass the form element to the constructor, so if `$('#claim-form')` is the form, `var claim = new FormData($('#claim-form')[0]);`, as shown in [Sending multipart/formdata with jQuery.ajax](https://stackoverflow.com/q/5392344/215552), assuming that `ajaxCall` is a proxy for jQuery's ajax... – Heretic Monkey Oct 29 '19 at 20:49
  • also in the ajax settings you might try adding `traditional: true` and in the actual controller endpoint maybe add `[FromBody]` to the method parameter – nulltron Oct 29 '19 at 20:49
  • Are you using ASP.NET Core? ASP.NET? WebForms? RazorViews? RazorPages? What version? – Ivan García Topete Oct 29 '19 at 20:52
  • ok i figured it out. just doing a plain ajax call rather than using our own ajax method worked; thanks! – Andra Avram Oct 29 '19 at 20:58

0 Answers0