jQuery Ajax error handling, show custom exception messages

前端 未结 21 3119
滥情空心
滥情空心 2020-11-22 01:50

Is there some way I can show custom exception messages as an alert in my jQuery AJAX error message?

For example, if I want to throw an exception on the server side v

21条回答
  •  执笔经年
    2020-11-22 02:34

    This function basically generates unique random API key's and in case if it doesn't then pop-up dialog box with error message appears

    In View Page:

    From Controller:

    public function regenerate(){
        $json = array();
        $api_key = substr(md5(rand(0,100).microtime()), 0, 12);
        $json['sync_id'] = $api_key; 
        $json['message'] = 'Successfully API Generated';
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }
    

    The optional callback parameter specifies a callback function to run when the load() method is completed. The callback function can have different parameters:

    Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )

    A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

提交回复
热议问题