OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Send data from client (vanilla js) to .net core API

  • Thread starter Thread starter rokdbest
  • Start date Start date
R

rokdbest

Guest
vanilla javascript client (I tried doing it with axios/ajax/fetch)

Code:
const myForm = document.getElementById("myForm");
myForm.addEventListener("submit", (e) => {
debugger;
var name = document.getElementById("errorInput").value;
var description = document.getElementById("errorDescriptionInput").value;
var date = document.getElementById("errorDateInput").value;
var mail = document.getElementById("senderEmailInput").value;
var file = document.getElementById('fileInput').files[0];

var formData = new FormData();
formData.append('fileInput', file);

const error = {
    ErrorName: name,
    ErrorDescription: description,
    ErrorSubmittedDate: date,
    SenderEmail: mail,
    Documents: formData,
}

axios.post("https://localhost:44310/api/Error/senderror","sendFile", error, {
    "Content-Type": "multipart/form-data",
    "Content-Type": "application/json",
})
})

.net controller API

Code:
[EnableCors("policy")]
[HttpPost]
[Route("senderror")]
public async Task<IActionResult> HandleError([FromForm]ErrorModel error)
{
   return Ok();
}

Browser debugger shows that the model contains the values, but the data that comes to the controller is null.

<p>vanilla javascript client (I tried doing it with axios/ajax/fetch)</p>
<pre><code>const myForm = document.getElementById("myForm");
myForm.addEventListener("submit", (e) => {
debugger;
var name = document.getElementById("errorInput").value;
var description = document.getElementById("errorDescriptionInput").value;
var date = document.getElementById("errorDateInput").value;
var mail = document.getElementById("senderEmailInput").value;
var file = document.getElementById('fileInput').files[0];

var formData = new FormData();
formData.append('fileInput', file);

const error = {
ErrorName: name,
ErrorDescription: description,
ErrorSubmittedDate: date,
SenderEmail: mail,
Documents: formData,
}

axios.post("https://localhost:44310/api/Error/senderror","sendFile", error, {
"Content-Type": "multipart/form-data",
"Content-Type": "application/json",
})
})
</code></pre>
<p>.net controller API</p>
<pre><code>[EnableCors("policy")]
[HttpPost]
[Route("senderror")]
public async Task<IActionResult> HandleError([FromForm]ErrorModel error)
{
return Ok();
}
</code></pre>
<p>Browser debugger shows that the model contains the values, but the data that comes to the controller is null.</p>
 

Latest posts

Top