Return Post Variables from c# api as JSON array

Call to c# api using ajax and recieve an array of variables in JSON format.

javascript call to api: In aspx page, you can access a posted in variable such as an id…

const NewsID = ‘<%=Request.Form[“NewsID”]%>’;

… then call to api and get record from that id…

 $.ajax({
type: 'POST',
url: 'update.aspx/loadRecord',
data: JSON.stringify({ NewsID: NewsID }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
var obj =  jQuery.parseJSON(msg.d)
console.log("I have successfully called to the api.");

 // set the heading value
 $("#Heading").val(obj.Heading);
 // set the body value
 $("#Body").val(obj.Body);
}
});


The C# api receives the record id and returns a record…

... call to database and get record ...

var message = new {Heading=Heading, Body= Body};
return JsonConvert.SerializeObject(message);

Leave a Reply