如何添加此脚本错误信息?

时间:2011-11-27 21:04:46

标签: javascript ajax

请帮帮我。如何添加此脚本错误信息(例如没有页面)?我想在装载机中查看此信息。

var AjaxContent = function(){
    var container_div = ''; 
    var content_div = ''; 
    return {
        getContent : function(url){
            $(container_div).animate({opacity:0}, //Turn the opacity to 0
                    function(){ // the callback, loads the content with ajax                        
                        $(container_div).find("#loading").show();
                        $(container_div).load(url+" "+content_div, //only loads the selected portion
                        function(){                        
                           $(container_div).animate({opacity:1}); //and finally bring back the opacity back to 1
                           $(container_div).find("#loading").hide();                           
                    }
                );        
            });
        },
        ajaxify_links: function(elements){
            $(elements).click(function(){
                $('#loading').show();
                AjaxContent.getContent(this.href);
                return false; //prevents the link from beign followed
            });
        },
        init: function(params){ //sets the initial parameters
            container_div = params.containerDiv; 
            content_div = params.contentDiv;
            return this; //returns the object in order to make it chainable
        }
    }
}()

/* Ajax Content Loading Controls */
$(function(){
        AjaxContent.init({containerDiv:".content_background",contentDiv:"#text"}).ajaxify_links("#nav a");
    });`

1 个答案:

答案 0 :(得分:2)

查看http://api.jquery.com/ajaxError/ ..首先,添加一个您要添加错误消息内容的元素

<div id="errors"></div>

然后添加一些javascript来捕获Ajax错误并写入此div

$( "#errors" ).ajaxError(function(e, jqxhr, settings, exception) {
  $(this).text("your error message");
});

或者如果您不想将ajaxError事件绑定到div,请使用以下代码:

$(document).ajaxError(function(e, jqxhr, settings, exception) {
    console.log("your error message");
});

或准确使用您的代码,试试这个:

var AjaxContent = function(){
    var container_div = ''; 
    var content_div = ''; 
    return {
        getContent : function(url){
            $(container_div).animate({opacity:0}, //Turn the opacity to 0
                    function(){ // the callback, loads the content with ajax                        
                        $(container_div).find("#loading").show();
                        $(container_div).load(url+" "+content_div, //only loads the selected portion
                        function(){                        
                           $(container_div).animate({opacity:1}); //and finally bring back the opacity back to 1
                           $(container_div).find("#loading").hide();                           
                    }
                );        
            });
        },
        ajaxify_links: function(elements){
            $(elements).click(function(){
                $('#loading').show();
                AjaxContent.getContent(this.href);
                return false; //prevents the link from beign followed
            });
        },
        init: function(params){ //sets the initial parameters
            container_div = params.containerDiv; 
            content_div = params.contentDiv;
            return this; //returns the object in order to make it chainable
        }
    }
}()

/* Ajax Content Loading Controls */
$(function(){
    $(document).ajaxError(function(e, jqxhr, settings, exception) {
        console.log("your error message");
    });
   AjaxContent.init({containerDiv:".content_background",contentDiv:"#text"}).ajaxify_links("#nav a");
});