So, I have an ASP.NET Generic handler defined with the following code snippet:
public void ProcessRequest ( HttpContext ctx ) {
try {
ctx.Response.ContentType = "text/json";
var jsonData = new StreamReader(ctx.Request.InputStream).ReadToEnd();
SearchRequest req = JsonConvert.DeserializeObject<SearchRequest> ( jsonData );
if ( jsonData == null || req == null ) {
ctx.Response.Write ( string.Empty );
return;
}
else {
var records = CustomerInvoiceDB.GetAll($"Invoice_ID = {req.SearchValue}");
var settings=new JsonSerializerSettings{DateFormatString ="mm/dd/yyyy"};
settings.NullValueHandling = NullValueHandling.Include;
var response = JsonConvert.SerializeObject(records, settings);
ctx.Response.Write ( response );
}
}
catch {
ctx.Response.Write ( null );
}
}
The data returned looks like this:
"[{\"Invoice_ID\":47390,\"Customer_ID\":15546,\"Invoice_Date\":\"00/01/2022\",\"Invoice_Total\":1892.5200,\"Customer_Order_ID\":12445,\"Due_Date\":\"00/01/2022\",\"Paid_Date\":null,\"Paid_Total\":0.0000,\"Is_Terms_Invoice\":false,\"Payment_Reference_No\":null,\"Notes\":null}]"
This web handler is called with the following $.ajax code:
$.ajax({
url: 'invoicedetailsearch.ashx',
data: json,
method: 'post',
dataType: 'json',
success: function (data) {
$('#mdlInvoiceNo').val(data.Invoice_ID);
$('#mdlInvoiceDate').val(data.Invoice_Date);
if (info.Is_Terms_Invoice == true) {
$('#mdlTerms').val('Yes');
}
else {
$('#mdlTerms').val('No');
}
$("#mdlInvoice").modal('show');
},
error: function (request, status, error) {
console.log(error);
}
});
Now, the weird thing is, the ajax call works, because it hits the success
function, but the data
returns undefined
.
I don't understand what's going on here. The web handler is returning JSON data, but the ajax call see the data returned as undefined. Any thoughts here?