我是新來jquery和json,我試圖找出爲什麼getAreas()函數返回500 /內部服務器錯誤 - 未定義。我檢查了WebMethod,它返回的數據和getRegions()函數工作得很好。 VS項目構建得很好。有任何想法嗎?代碼如下:ajax jquery json返回500內部服務器錯誤(未定義),但webmethod工程
C#服務器端
[WebMethod]
public static ArrayList GetRegionsArrayList()
{
ArrayList arrayList = new ArrayList();
foreach (DataRow dr in Utility.Regions().Rows)
{
arrayList.Add(new ListItem(dr["Region"].ToString(), dr["Dot4"].ToString()));
}
return arrayList;
}
[WebMethod]
public static ArrayList GetAreasArrayList(string Dot4)
{
ArrayList arrayList = new ArrayList();
foreach (DataRow dr in Utility.Areas(Dot4).Rows)
{
arrayList.Add(new ListItem(dr["Area"].ToString(), dr["Dot6"].ToString()));
}
return arrayList;
}
的JavaScript
<script type="text/javascript" language="javascript">
function PopulateControl(list, control) {
if (list.length > 0) {
control.removeAttr("disabled");
control.empty().append('<option selected="selected" value="0">Please select</option>');
$.each(list, function() {
control.append($("<option></option>").val(this['Value']).html(this['Text']));
});
} else {
control.empty().append('<option selected="selected" value="0">Not available<option>');
}
}
function getRegions() {
$.ajax({
type: "POST",
url: "Demo.aspx/GetRegionsArrayList",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnRegionsPopulated,
error: function (response) {
alert(response.status + ' ' + response.statusText);
},
failure: function (response) {
alert(response.d);
}
});
}
function getAreas() {
$.ajax({
type: "POST",
url: "Demo.aspx/GetAreasArrayList",
data: "{Dot4: ' + $('#<%=DDL_Region.ClientID%>').val() + '}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnAreasPopulated,
error: function (response) {
alert(response.status + ' ' + response.statusText);
},
failure: function (response) {
alert(response.d);
}
});
}
function OnRegionsPopulated(response) {
PopulateControl(response.d, $("#<%=DDL_Region.ClientID%>"));
}
function OnAreasPopulated(response) {
PopulateControl(response.d, $("#<%=DDL_Area.ClientID%>"));
}
</script>
控制
<select id="Select1" onchange="getRegions();">
<select id="DDL_Region" onchange="getAreas();" runat="server"></select>
<select id="DDL_Area" runat="server"></select>
錯誤信息:
{「Message」:「無效的JSON基元:Dot4。」,「StackTrace」:「at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\ r \ n at System.Web.Script.Serialization。 JavaScriptObjectDeserializer.DeserializeInternal(的Int32 深度個)\ r \ n在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串 輸入,的Int32 depthLimit,JavaScriptSerializer串行個)\ r \ n在 System.Web.Script.Serialization。 JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer,String input,Type type,Int32 depthLimit)\ r \ n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize [T](String input)\ r \ n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext的 上下文,JavaScriptSerializer串行個)\ r \ n在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext的上下文)\ r \ n在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext的背景下 ,WebServiceMethodData methodData)」, 「ExceptionType」: 「System.ArgumentException」}
500意味着服務器端有問題 – Rafay 2012-03-16 16:31:23
服務器錯誤的詳細信息是什麼? – Paul 2012-03-16 16:32:45
我測試了web方法並返回數據。基本上,這些是級聯下拉列表。調用getRegions()的方法工作得很好。 - 它返回undefined。我不知道如何提取更具體的錯誤信息。 – xizwyck 2012-03-16 16:32:46