JQuery AJAX响应在C#ASP.NET中仅返回[object]且未定义
我已经看到这个问题被问了很多次,但请试着理解我的实际问题是什么JQuery AJAX响应在C#ASP.NET中仅返回[object]且未定义,jquery,asp.net,ajax,json,Jquery,Asp.net,Ajax,Json,我已经看到这个问题被问了很多次,但请试着理解我的实际问题是什么 我的JavaScript代码是: $.ajax({ type: "POST", url: 'ScheduleCampiagn.aspx/GetTemplate', data: '{TempId: ' + $('#<%=ddl_Select_Template.ClientID%>').val() + '}', con
我的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) {
}
});
请在这方面帮助我,或者请让我知道我在这些代码中遗漏了什么,我已经通过搜索丢失了一整天
非常感谢响应被包装在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) {
}
});
由于您有firebug,您可以使用
console.log
而不是alert
来查看实际对象包含的内容-然后在firebug控制台中您可以看到该对象。您还可以在网络选项卡>查找ajax调用>查看响应我认为您需要从webmethod
返回类型为json
的响应,因为您的ajax期望相同…是的,我得到了一个错误,即ReferenceError:响应未定义您可以在调试窗口中看到实际的对象(对于chrome,为F12),在alert.try alert(response.d[0].TempId)之前放置断点vbefore alert.try alert;我没有看到更改背后的代码。我想他无论如何都想返回列表是的,它工作得很好,非常感谢,但我怎么也没有正确理解@gurupasad给出的列表
1.dataTemp.TempId
2.dataTemp.TempContent
$.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;
}