Hello wenn i want to send a post request to my Controller there is no data. I tried to log my Json file and there is something. But when I send the post request my controller shows it is empty.
Here is my call:
var jsonString = JSON.stringify(jsonObj);
console.log("jsonString : " jsonString);
$.ajax({
url: "/Admin/SaveProductToDB",
type: "POST",
data: { dataToSend: jsonString},
success: function (data) {
if (data.status == "Success") {
BootstrapDialog.show({
title: 'Success!',
message: "Data Updated Successfully!",
buttons: [{
label: 'OK',
action: function (dialog) {
window.location.href = "/Admin/Product";
removeProdData(i);
$("#btnAddProd").attr("disabled",false);
dialog.close();
}
}]
});
}
}
});
//Here I make a breakpoint but my string is empty
public JsonResult SaveProductToDB(string dataToSend)
{
List<Product> _List = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Product>>(dataToSend);
}
I use a similar code on another place and there it sends the data.
CodePudding user response:
You are checking against data.status
as if it's a given that it exists. Just console.log(data)
instead and you will see whether or not status is being returned.
Also, if you open the Network tab in Chrome you can click on the post request & see if your headers are going through accurately and also click on 'Preview' to see an unfiltered result from the controller.
CodePudding user response:
you must test your api '/Admin/SaveProductToDB' with rest client software as Insomnia or Postman for example.