0

I'm working on a project where I need to send some data to my SQL Server using ASP.NET Core on server side. On Client side I'm using Javascript to run my application that send me an Array of Objects that I need to save. So, I thought that using Ajax to send the Data to my Controller ,and then sending it to my server (using EF), could be a good idea. The problem is that I cannot even send a string like : var x = "first try"; to my Controller because I have this error (400) : Error Parsing XML : element not found. I tried multiple things like removing datatype : 'text' or datatype: 'json' or forcing the MIMETYPE to text.

Here is what I have done :

View :

function save (){
   var jsonStringify = JSON.stringify({value : "yo"});
   $.ajax({
      type : 'POST',
      url : "@Url.Action("saveChart")",
      data : {"jsonData" : jsonStringify},
      datatype : "text"
   });
}

I already tried to change data type,Url. My path for the controler = MyWebsite/HomeController/saveChart.

Controller :

[HttpPost]
public IActionResult  saveChart(string jsonData){
   ChartClass chart = new ChartClass();
   int nbr = _connection.Chart.Count();
   chart.value = "check connection controler";
   chart.id = nbr + 1;
   this._connection.Chart.Add(chart);
   this_connection.SaveChanges();
   return View("Views/Home/Chart.cshtml");
}

I tried to put [FromBody] , still nothing.

Model :

public class ChartClass(){
   [Key]
   public int id {get; set;}

   public string valeur {get; set;}
}

But I can acces to my database and so, to my controller using a html form with asp-action="saveChart" asp-controller="Home"

1 Answer 1

1

The default contentType would be application/x-www-form-urlencoded; charset=UTF-8, to make Ajax request(s) with posted data to your saveChart action method, you can try:

$.ajax({
    type: 'POST',
    url: "@Url.Action("saveChart")",
    data: {
        "jsonData": "yo"
    },
    datatype: "text"

    //... 

Test Result

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

You seems not use the parameter jsonData in your action code logic. So is the code just for testing purpose?
Yes, I just wanted to know if my Ajax call was operational. It did work by adding (I had to) : beforeSend : function(xhr){ xhr.setRequestHeader("RequestVerificationToken",$('input:hidden[name="__RequestVerificationToken"]').val()) } in my Ajax call. And [ValidateAntiForgeryToken] in my Controller. But now, my jsonData is null if I leave ContentType :'application/json'. Why ? Am I not sending a json with data : {"jasonData" : "yo"} ?
The antiforgery token could prevent CSRF attack, if the token is invalid, which might cause HTTP 400 error, it should not cause model binding wrong.
my jsonData is null if I leave ContentType :'application/json'. Why ? The data would be passed through request body, you can try to apply [FromBody] attribute to parameter saveChart([FromBody]string jsonData). And modify ajax data option with data: JSON.stringify("yo").
Hi @Crakenar, if the solution did help resolve the issue, kindly accept it as answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.