How to trigger jquery.ajax() error callback based on server response, not HTTP 500?

后端 未结 6 1151
清酒与你
清酒与你 2020-11-30 03:32

By using jquery ajax function, I can do something like:

$.ajax({

  url: url,
  type: \'GET\',
  async: true,
  dataType: \'json\',
  data: data,

 success:          


        
6条回答
  •  清歌不尽
    2020-11-30 04:06

    This original question was posted years ago, and perhaps there are better ways of handling this now. Here's a suggestion to handle multiple types of errors, including the OP's question.

    Input type (error) example #1

    function handleError (type){
    type = type || "Connection Issue";
     var value = "Error occurred: " + type;
     alert(value);
    }
    
    function checkIfError(response){
        if (response == "DB_ERROR") {
            handleError("Server error");
            throw new Error("JS died"); // optional, keep from running additional script
         }
    }
    
    var example = {
    
        success: function (response) {
            checkIfError(response); // check data if error msg
            // do stuff when proper data sent
            alert(response);
    
        }, error: handleError // connection error, call error msg
    };
    
    test1 = example['error']();

    Input type (success:error) example #2

    function handleError (type){
    type = type || "Connection Issue";
     var value = "Error occurred: " + type;
     alert(value);
    }
    
    function checkIfError(response){
        if (response == "DB_ERROR") {
            handleError("Server error");
            throw new Error("JS died"); // optional, keep from running additional script
         }
    }
    
    var example = {
    
        success: function (response) {
            checkIfError(response); // check data if error msg
            // do stuff when proper data sent
            alert(response);
    
        }, error: handleError // connection error, call error msg
    };
    
    
    test2 = example['success']("DB_ERROR");

    Input type (success) example #3

    function handleError (type){
    type = type || "Connection Issue";
     var value = "Error occurred: " + type;
     alert(value);
    }
    
    function checkIfError(response){
        if (response == "DB_ERROR") {
            handleError("Server error");
            throw new Error("JS died"); // optional, keep from running additional script
         }
    }
    
    var example = {
    
        success: function (response) {
            checkIfError(response); // check data if error msg
            // do stuff when proper data sent
            alert(response);
    
        }, error: handleError // connection error, call error msg
    };
    
    
    test3 = example['success']("Proper Response");

    By using functions, if you have multiple calls in your scripts, you can simplify changes.

    Have your PHP (or whatever server side code) return the response of "DB_ERROR" (or whatever you want) to trigger the error; allows you to do different things depending on the error.

提交回复
热议问题