2014-05-19 30 views
0

我使用jQuery的ajax下拉更改函數。問題是,即使在點擊提到的阿賈克斯請求中的網址,我越來越Object object error對象在下拉更改時發生jquery ajax請求上的錯誤?

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格式正確地返回。

由於錯誤顯示的URL被擊中之前也,我對如何進一步進行混淆。

任何幫助將不勝感激。

+0

嘗試更改.aspx頁面context.Response.ContentType =「application/json」; –

+0

你也可以通過網頁信息檢查你的頁面內容類型。爲此,您必須在瀏覽器中打開.aspx頁面,右鍵單擊頁面並選擇「查看頁面信息」。 –

+0

嘗試了ur方法,但仍然收到相同的錯誤。我還檢查了頁面信息,結果如下所示:Type = Text/Html; Encoding = UTF-8' – SparAby

回答

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;  
} 

你不必序列化這些名單

+0

任何解釋爲什麼OP應該這樣做?他已經聲明_我檢查過字符串sjson並且數據以json格式正確返回._ – Tupel

+0

但他說:」問題是,即使在擊中ajax請求中提到的網址我得到對象對象錯誤「。這可能是因爲contentType –

+0

中的不匹配嘗試了上面的代碼,但仍然出現錯誤。 – SparAby

0

問題解決了,感謝每一個誰特別是回答@Anoop。

問題是我已經設置了Autopostback=true的下拉列表中的ajax調用。我已經刪除了autopostback屬性的下拉菜單,現在代碼工作正常。

我不知道如何清新的一天,清晰的頭腦有助於解決問題。

相關問題