下拉列表更改时jquery ajax请求上的对象对象错误?

时间:2014-05-19 06:58:45

标签: c# javascript jquery asp.net ajax

我在下拉更改功能上使用了jquery ajax。问题是,即使在点击ajax请求中提到的url之前,我还是Object object error

ajax请求如下

 $("#locationList").change(function () {
             var locationNo = document.getElementById('<%=locationList.ClientID%>').value;
             $.ajax({
                 url: "HealthReport.aspx/GetCashsafes",
                 data: "{ 'Location': '" + locationNo + "'}",
                 type: "POST",
                 dataType: "json",
                 contentType: "application/json; charset=utf-8",
                 success: function (data) {
                     alert("Success");
                     response($.each(data.d, function (key, value) {                        
                     $("#CashSafeList").append($("<option></option>").val(value.CashsafeId).html(value.CashsafeDisplaySerialNo));
                     }));
                 },
                 error: function (result) {
                     alert(result);
                     $("#CashSafeList").append($("<option></option>").val("-1").html("Select one"));
                 }
             });
         });

服务器端代码如下

 [WebMethod]       
    public static string GetCashsafes(string Location)
    {
        Decimal userId = (Decimal)AMSECSessionData.userId;
        List<Cashsafe> lstCashSafe = DropDown.getCashSafeListLocationwise(userId, Convert.ToDecimal(Location));
        List<CashSafeSelect> lstCashSafeSelect = new List<CashSafeSelect>();
        lstCashSafeSelect = lstCashSafe.Select(item => new CashSafeSelect()
        {
            CashsafeId=(decimal)item.CashsafeId,
            CashsafeSerialNo=item.CashsafeSerialNo.ToString()

        }).Distinct().ToList();
        System.Web.Script.Serialization.JavaScriptSerializer jSearializer =
              new System.Web.Script.Serialization.JavaScriptSerializer();
        string sjson=jSearializer.Serialize(lstCashSafeSelect);
        return sjson;     
    }

我检查了string sjson,数据以json格式正确返回。

由于错误在网址被点击之前显示,我对如何进一步处理感到困惑。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

像这样更改data

data: JSON.stringify({ 'Location':  locationNo }),

然后你的代码看起来像

$("#locationList").change(function () {
         var locationNo = document.getElementById('<%=locationList.ClientID%>').value;
         $.ajax({
             url: "HealthReport.aspx/GetCashsafes",
             data: JSON.stringify({ 'Location':  locationNo }),
             type: "POST",
             dataType: "json",
             contentType: "application/json; charset=utf-8",
             success: function (data) {
                 alert("Success");
                 response($.each(data.d, function (key, value) {                        
                 $("#CashSafeList").append($("<option></option>").val(value.CashsafeId).html(value.CashsafeDisplaySerialNo));
                 }));
             },
             error: function (result) {
                 alert(result);
                 $("#CashSafeList").append($("<option></option>").val("-1").html("Select one"));
             }
         });
     });

修改

由于您的dataType是json,因此您应该返回json,而不是string。像这样更改服务器端代码,

[WebMethod]       
public static List<CashSafeSelect> GetCashsafes(string Location)
{
    Decimal userId = (Decimal)AMSECSessionData.userId;
    List<Cashsafe> lstCashSafe = DropDown.getCashSafeListLocationwise(userId, Convert.ToDecimal(Location));
    List<CashSafeSelect> lstCashSafeSelect = new List<CashSafeSelect>();
    lstCashSafeSelect = lstCashSafe.Select(item => new CashSafeSelect()
    {
        CashsafeId=(decimal)item.CashsafeId,
        CashsafeSerialNo=item.CashsafeSerialNo.ToString()

    }).Distinct().ToList();

    return lstCashSafeSelect;     
}

您不必序列化这些列表

答案 1 :(得分:0)

问题解决了,感谢每个回复特别是@Anoop的人。

问题是我为进行ajax调用的下拉列表设置了Autopostback=true。我已删除了下拉列表的autopostback属性,现在代码工作正常

我想知道一个新的一天,清醒的头脑有助于解决问题。