我有一个get ajax调用:
function TraerInstructivos() {
$.ajax({
type: "GET",
url: '<%= Page.ResolveUrl("~/Instructivo/Instructivos.aspx") %>' + '/TraerInstructivos',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$.each(result, function (i, item) {
alert(item.DescripcionVideo);
alert(item.DireccionVideo);
});
},
error: function (response) {
alert("Error");
}
});
};
这在我的aspx中调用了以下webmethod:
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static List<InstructivoDTO> TraerInstructivos()
{
try
{
return Controles_Instructivo_Instructivos.TraerInstructivos();
}
catch (Exception ex)
{
throw ex;
}
}
这会在我的ascx中调用一段代码:
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static List<InstructivoDTO> TraerInstructivos()
{
List<InstructivoDTO> lstResponse = new List<InstructivoDTO>();
WC.InstructivoDTOResp response = new WC.InstructivoDTOResp() { ListaDeInstructivos = new WC.ListaDeInstructivos() };
//Traigo los instructivos
WC.InstructivoDTOReq request = new WC.InstructivoDTOReq()
{
TipoOperacion = WC.Accion.Consultar,
Operacion = Constantes.Consultas.Instructivos.TRAER_INSTRUCTIVOS_WEB_COMERCIO,
ListaDeInstructivos = new WC.ListaDeInstructivos()
};
using (WC.FacturaClient fc = new WC.FacturaClient())
{
response = fc.InstructivosEjecutar(request);
}
foreach (var i in response.ListaDeInstructivos)
{
lstResponse.Add(new InstructivoDTO()
{
DescripcionVideo = i.DescripcionVideo,
DireccionVideo = i.DireccionVideo,
EsBackOffice = i.EsBackOffice
});
}
return lstResponse;
}
返回一个POCO对象或DTO列表,真正的简单,它有3个属性,其中2个是字符串类型,另一个是布尔值。
在我的ajax调用的alert函数中,我看到我收到'undefined'作为结果。
我错过了什么吗?我试过stringify
,JSON.Parse(response.d)
说'无效字符'。
编辑:
感谢HaukurHaf的响应,我改变了我的jquery中的for循环,似乎当我做一些测试时我改变了它,所以我的Ajax是:
<script type="text/javascript">
$(document).ready(function () {
TraerInstructivos();
});
function TraerInstructivos() {
$.ajax({
type: "GET",
url: '<%= Page.ResolveUrl("~/Instructivo/Instructivos.aspx") %>' + '/TraerInstructivos',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$.each(response, function (i, item) {
alert(item.DescripcionVideo);
alert(item.DireccionVideo);
});
},
error: function (response) {
alert("Error");
}
});
};
</script>
仍然未定义,这是有趣的部分,如果我将一个console.log而不是警报放到我的整个对象,我可以看到它与我放在我的表上的值:
我找到了答案。
看来我缺少整个解决方案的另一半。它不工作的原因,这是因为我说我将一个Json对象返回到ajax调用,而是我返回对象而不序列化它们。所以现在代码看起来像这样:
ASPX:
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string TraerInstructivos()
{
try
{
List<InstructivoDTO> response = Controles_Instructivo_Instructivos.TraerInstructivos();
var json = new JavaScriptSerializer().Serialize(response);
return json;
}
catch (Exception ex)
{
throw ex;
}
}
AJAX电话:
function TraerInstructivos() {
$.ajax({
type: "GET",
url: '<%= Page.ResolveUrl("~/Instructivo/Instructivos.aspx") %>' + '/TraerInstructivos',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var res = JSON.parse(response.d);
$.each(res, function (i, item) {
alert(item.DescripcionVideo);
});
},
error: function (response) {
alert("Error");
}
});
};