我使用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被擊中之前也,我對如何進一步進行混淆。
任何幫助將不勝感激。
嘗試更改.aspx頁面context.Response.ContentType =「application/json」; –
你也可以通過網頁信息檢查你的頁面內容類型。爲此,您必須在瀏覽器中打開.aspx頁面,右鍵單擊頁面並選擇「查看頁面信息」。 –
嘗試了ur方法,但仍然收到相同的錯誤。我還檢查了頁面信息,結果如下所示:Type = Text/Html; Encoding = UTF-8' – SparAby