mikha
asked on
ajax-api request/response
I am designing an api, which will be called from my front end. say I have a controller method that calls some other third party api process results and returns json response to the front end ( see sample code below) .
if there is an error in the reques/response and the request is unsucessful to the 3rd party apis, what is best approach. should I return empty json response to the front end , after handling and logging exceptions.
or is it better to send some message so that it can be dealt on the front end. if so can someone share some sample code for this. ?
//get method in some controller
public ActionResult GetData()
{
try
//call other apis
//if the response is successful
return data;
//if unsucessful
// ?? what to return to the front end ??
catch
//log exceptions
}
front end code -------------------------- ---------- ----
$(document).ready(function () {
$.ajax({
type: "GET",
url: "//somecontroller//GetMeth od",
data: param = "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
//process json data
}
function errorFunc() {
//do something
}
});
if there is an error in the reques/response and the request is unsucessful to the 3rd party apis, what is best approach. should I return empty json response to the front end , after handling and logging exceptions.
or is it better to send some message so that it can be dealt on the front end. if so can someone share some sample code for this. ?
//get method in some controller
public ActionResult GetData()
{
try
//call other apis
//if the response is successful
return data;
//if unsucessful
// ?? what to return to the front end ??
catch
//log exceptions
}
front end code --------------------------
$(document).ready(function
$.ajax({
type: "GET",
url: "//somecontroller//GetMeth
data: param = "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
//process json data
}
function errorFunc() {
//do something
}
});
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes, that’s the usual way. Sending a 500 error and an explanation should be easy to handle in the error handler to show the end user
ASKER