REST api调用失败时更改默认服务不可用消息

时间:2014-05-22 13:35:58

标签: java jquery ajax rest tomcat

当用户尝试进行api调用但是如果我们的服务器关闭(由于许多原因),那么我的网站UI会出现以下错误:

  

服务不可用

     

由于服务器暂时无法为您的请求提供服务   维护停机或容量问题。请稍后再试。   此外,尝试时遇到406 Not Acceptable错误   使用ErrorDocument来处理请求。

     

Apache / 2.4.7(Win32)OpenSSL / 1.0.1e本地主机端口80的PHP / 5.5.6服务器

我不希望这条消息如此详细。我想自定义此消息。我怎样才能做到这一点?如果您建议更改我的ajax请求(从我做api调用的地方),那就更好了

以下是我执行所有api请求的javascript(如果有帮助的话)

function request(url, request_type, datatype, callback, postdata){

        if(typeof(postdata) == "object"){
            postdata = JSON.stringify(postdata);
        } else if(typeof(postdata) == "string") {
            SY.setContentType("text/plain");
        }

        $.ajax({
            beforeSend: function(xhr){
                for(var key in requestHeaders){
                    if(key == ANOTHER_CONTENT_TYPE)
                        continue;

                    xhr.setRequestHeader(key, requestHeaders[key]);    
                }

                var client_id = readCookie(CLIENT_ID);
                var client_token = readCookie(CLIENT_TOKEN);
                if(client_id != null && client_token != null){
                    xhr.setRequestHeader(CLIENT_ID, client_id);
                    xhr.setRequestHeader(CLIENT_TOKEN, client_token);
                }

                //if there is another content type defined then modify the xhr content-type header
                var newContentType = requestHeaders[ANOTHER_CONTENT_TYPE];
                if(newContentType != undefined) {
                    SyUtils.log("Changing request content type to ="+newContentType);
                    xhr.setRequestHeader("Content-Type", newContentType);
                    delete requestHeaders[ANOTHER_CONTENT_TYPE];
                }

                var loadingDivId = callback.loadingDivId;
                if(loadingDivId){
                    $("#"+loadingDivId).addClass("loader-small");
                }else{
                    $("body").append("<div id=\"loader\"></div>");
                }

            },
            url: url,
            data: postdata,
            dataType: datatype,
            type: request_type,
            success: function(data) {

                var loadingDivId = callback.loadingDivId;
                if(loadingDivId){
                    $("#"+loadingDivId).removeClass("loader-small");
                }else{
                    $("#loader").remove();
                }

                // Write back cookie again so that expiry time refreshes
                var client_id = readCookie(CLIENT_ID);
                var client_token = readCookie(CLIENT_TOKEN);
                if(client_id != null && client_token != null){
                    saveCredentials(client_id, client_token);
                }
                callback("success", data);
            },
            error: function(xhr, status, error) {
                var loadingDivId = callback.loadingDivId;
                if(loadingDivId){
                    $("#"+loadingDivId).removeClass("loader-small");
                }else{
                    $("#loader").remove();
                }
                try{
                    var errorMessageObj = JSON.parse(xhr.responseText);
                }catch(err){
                    errorMessageObj = {type:"ERROR", message:xhr.responseText};
                }

                var st = xhr.status;
                var currentPage = window.location.pathname; // to avoid infinite redirect loop
                if (st == 401 && currentPage != "/login"){
                    SY.removeCurrentUser();
                    deleteCookie(CLIENT_ID);
                    deleteCookie(CLIENT_TOKEN);
                    window.location.replace("/login?toPage="+currentPage);
                }else if(st == 403 && currentPage != "/unauthorized"){
                    window.location.replace("/unauthorized");
                }else if(st == 500 && currentPage != "/error"){
                    window.location.href = "/error";
                }else if(st == 404 && currentPage != "/404"){
                    window.location.href = "/404";
                }else{
                    callback(error, errorMessageObj);
                }   
            }
        });
    };

0 个答案:

没有答案