I am facing an issue with POSTing a string data to the api controller in mvc using ajax. Despite my efforts, the data does not seem to reach the api controller. Here is what I have attempted:
This is the JavaScript code I have used:
jQuery(function () {
$(function () {
$(".listing-listing").html("");
const jsonData = { PageNom: 1, ShowInPage: 25 };
var data = JSON.stringify(jsonData);
$.ajax({
url: '/Api/apiListProduct/AjaxSearchAsync',
type: 'POST',
data: data,
dataType: 'jsonResult',
contentType: "application/json; charset=utf-8",
success: function (result) {
if (result.Data.Succeed) {
result.Data.ProductLst.forEach((product) => AddProduct(product));
//$('.PaginationCell').html(result.Data.Pagination);
}
},
error: function (e) {
// handle error
}
});
});
});
And here is the corresponding api controller:
public class apiListProduct : ApiController {
[System.Web.Http.HttpPost]
public async System.Threading.Tasks.Task<System.Web.Mvc.JsonResult> AjaxSearchAsync()
{
string raw = "";
using (var contentStream = await Request.Content.ReadAsStreamAsync())
{
contentStream.Seek(0, System.IO.SeekOrigin.Begin);
using (var sr = new System.IO.StreamReader(contentStream))
{
raw = sr.ReadToEnd();
}
}
}
}
Your assistance in resolving this issue with the web API route for the api controller will be greatly appreciated. Thank you.