我已经看到这个问题被问了很多次,但请尝试了解我的实际问题是什么。
<小时/>我的 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.执行时显示警告框,内容为[object Object]。
2.当我将成功函数更改为 警报(响应[0].TempId); 它表明响应是不可防御的
3.i还使用FireBug调试js代码,它显示ReferenceError:response is undefiened。
4.i也尝试使用response.d来获取值,但它不起作用。
我只想获取 dataTemp i:e 的内容
<小时/> 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;
}
关于JQuery AJAX 响应仅返回 [object Object] 且在 C# ASP.NET 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30051676/