如何在ajax调用中获取错误描述

时间:2014-04-08 18:26:26

标签: jquery ajax jquery-ui

我有以下代码通过.ajax call. When获取一些数据.ajax executes it redirects to an error` handler。

 $(document).ready(function(){ 
    $('#ctl00_ContentMain_ddlRegions').change(function(){
        region = $(this).val();
        alert(region);              
    });         
        $('input[type="text"]').autocomplete({ 
          source: function(request, response)
          {
            $.ajax({
                url: "EmailActivation/EmailActivation.asmx/GetIsoFromRegion",
                data: "{ 'region': '" + region + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function(data){
                    alert("success");
                },
                error: function(response){
                    alert("error: fails");
                }, 
                failure: function(response){
                    alert("failure: fails");
                }                      
            });
          },
          select: function(e,i){
            $("#<%=hfIso.ClientID %>").val("retrieved"); 
          },
          minLength: 4  
        });  
});

我怎样才能知道我得到了什么错误,所以我可以解决所发生的一切?

谢谢

2 个答案:

答案 0 :(得分:0)

这不是有效的json。根据经验,永远不要通过连接构建json字符串,而是使用库:

var data = { "region" : region };
var json = JSON.stringify(data);

至于如何找到错误,您可以先查看网络标签。我认为它是来自服务器的500错误,可以通过记录或调试找到。

答案 1 :(得分:0)

我发现了错误:我添加了以下代码:

   $(document).ajaxError(function(e, xhr, opt){
      alert(xhr.status + " " + xhr.statusText);
    });

感谢您输入的人。

相关问题