【问题标题】:bootstrap typehead ajax error handling引导 typeahead ajax 错误处理
【发布时间】:2013-12-31 17:29:48
【问题描述】:

以下代码运行良好。我想处理错误情况,例如如果服务器的结果是空对象或搜索时出现一些数据库错误,我应该如何捕捉错误并显示一些消息?

var nameIdMap = {};

        $('#selectAgent').typeahead({
        source: function (query, process) {
        var that = this;
            return $.ajax({
                dataType: "json",
                url: '/eBus/EbusinessAgentServlet',
                data: 'q=' + query +"&task=tab1&AgentId=1234",                                      
                type: 'POST',
                beforeSend: function() {
                //that.$element is a variable that stores the element the plugin was called on
                    that.$element.addClass('loading');
                },

                complete: function() {
                that.$element.removeClass('loading');
                },

                success: function (json) {
                console.log(json)
                    process(getOptionsFromJson(json));
                }
            });
        },
        minLength: 2,
        updater: function (item) {
            console.log('selected id'+nameIdMap[item]);
            return item;
        }
    });


});

 function getOptionsFromJson(json) {
$.each(json, function (i, v) {
    nameIdMap[v.fname + " "+ v.lname + " " + v.agentID] = v.agentID;
});

return $.map(json, function (n, i) {
    return n.fname +" " + n.lname+" " + n.agentID;
});
}

【问题讨论】:

    标签: jquery bootstrap-typeahead


    【解决方案1】:

    您可以使用fail() 方法或statusCode 选项进行错误处理,如下所示:

    return $.ajax({
      dataType: "json",
      url: '/eBus/EbusinessAgentServlet',
      data: 'q=' + query +"&task=tab1&AgentId=1234",                                      
      type: 'POST',
      beforeSend: function() {
        //that.$element is a variable that stores the element the plugin was called on
        that.$element.addClass('loading');
      },
      complete: function() {
         that.$element.removeClass('loading');
      },
      success: function (json) {
         console.log(json)
         process(getOptionsFromJson(json));
      })
      .fail(function() {
        alert( "error" );
      });
    

    return $.ajax({
      dataType: "json",
      url: '/eBus/EbusinessAgentServlet',
      data: 'q=' + query +"&task=tab1&AgentId=1234",                                      
      type: 'POST',
      beforeSend: function() {
        //that.$element is a variable that stores the element the plugin was called on
        that.$element.addClass('loading');
      },
      complete: function() {
         that.$element.removeClass('loading');
      },
      success: function (json) {
         console.log(json)
         process(getOptionsFromJson(json));
      }
      statusCode: {
        404: function() {
         alert( "page not found" );
      }
    });
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 2012-02-25
      • 2013-08-29
      • 2011-03-25
      相关资源
      最近更新 更多