Link to home
Start Free TrialLog in
Avatar of mikha
mikhaFlag for United States of America

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//GetMethod",
                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
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mikha

ASKER

@Michel - thanks . how should I handle this 50x codes on the front end. or say how is this is usually done.   as I am calling a third party API, is it better that I catch any exceptions and just return one generic message in all situations, to my front end or should I program for different situations. what is better way of handling this?
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