[英]JQuery AJAX response returning only [object Object] and Undefined in C# ASP.NET
我已經多次詢問過這個問題了,但請盡量了解我的實際問題是什么。
我的JavaScript代碼是:
$.ajax({
type: "POST",
url: 'ScheduleCampiagn.aspx/GetTemplate',
data: '{TempId: ' + $('#<%=ddl_Select_Template.ClientID%>').val() + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
failure: function (response) {
}
});
ScheduleCampiagn.aspx.cs中的GetTemplate函數是:
[System.Web.Services.WebMethod]
public static List<TemplateClass> GetTemplate(int TempId)
{
List<TemplateClass> dataTemp = new List<TemplateClass>();
TemplateClass temp = new TemplateClass();
String cmd = "Select Tmpl_Id,Tmpl_Body_Content from TBL_DESIGN_TEMPLETE WHERE Tmpl_Id='" + TempId + "'";
DataSet dsTemp = new DataSet();
dsTemp.Clear();
con.Retrive(cmd, ref dsTemp);
cmd = string.Empty;
temp.TempId = Convert.ToInt32(dsTemp.Tables[0].Rows[0]["Tmpl_Id"]);
temp.TempContent = Convert.ToString(dsTemp.Tables[0].Rows[0]["Tmpl_Body_Content"]);
dataTemp.Add(temp);
return dataTemp;
}
GetTemplate函數只返回我預期的一行。 但我的問題是:
1.當它執行顯示內容為[對象對象]的警告框時。
2.當我將成功函數更改為alert(response [0] .TempId); 它表明反應是不妥的
3.i還使用FireBug調試js代碼它顯示了ReferenceError:響應未定義。
4.i也嘗試使用response.d來獲取值,但它不起作用。
1.dataTemp.TempId
2.dataTemp.TempContent
請幫助我,或者讓我知道我在這些代碼中遺漏了什么,我已經通過搜索丟失了整整一天。
非常感謝你
響應包含在d屬性中。
$.ajax({
type: "POST",
url: 'ScheduleCampiagn.aspx/GetTemplate',
data: '{TempId: ' + $('#<%=ddl_Select_Template.ClientID%>').val() + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
// fail-safe for older ASP.NET frameworks
var data = response.hasOwnProperty("d") ? response.d : response;
alert(data.TempId); //Changed here
},
failure: function (response) {
}
});
代碼背后的方法。
//Changed the return type to a single object instead of list.
[System.Web.Services.WebMethod]
public static TemplateClass GetTemplate(int TempId)
{
TemplateClass temp = new TemplateClass();
String cmd = "Select Tmpl_Id,Tmpl_Body_Content from TBL_DESIGN_TEMPLETE WHERE Tmpl_Id='" + TempId + "'";
DataSet dsTemp = new DataSet();
dsTemp.Clear();
con.Retrive(cmd, ref dsTemp);
cmd = string.Empty;
temp.TempId = Convert.ToInt32(dsTemp.Tables[0].Rows[0]["Tmpl_Id"]);
temp.TempContent = Convert.ToString(dsTemp.Tables[0].Rows[0]["Tmpl_Body_Content"]);
return temp;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.