JSON调用返回'未定义'
JSON call returning 'undefined'
我对使用JSON还比较陌生,遇到了一个似乎无法解决的问题。我已经设置了一个通用处理程序(下面详细介绍),还有一个带有JSON调用的页面,用于从处理程序中获取响应并进行呈现
带有JSON调用的页面是:
<script type="text/javascript">
$(document).ready(function () {
triggerCall();
function triggerCall() {
$.ajaxSetup({
type: 'POST',
headers: { "cache-control": "no-cache" }
});
$.ajax
(
{
type: "POST",
url: "Services/DepartmentListHandler.ashx",
contentType: "application/json;charset=utf-8",
dataType: "json",
async: false,
cache: false,
success: function (data) {
alert(data.msg);
displayDirectory(data.msg);
},
error: function (x, e) {
alert("The call to the server side failed. " + x.responseText);
}
}
);
}
function displayDirectory(dirlist) {
var tablestring = '<table class="centered-table"><tr><td colspan="2">';
tablestring = tablestring + "<tr><td><u>DeptCode</u></td><td><u>HeaderText</u></td></tr>";
for (var i = 0, len = dirlist.length; i < len; ++i) {
tablestring = tablestring + "<tr>";
tablestring = tablestring + " <td>" + dirlist[i].DeptCode + "</td>";
tablestring = tablestring + " <td>" + dirlist[i].HeaderText + "</td>";
tablestring = tablestring + "</tr>";
}
tablestring = tablestring + "</table>";
$('#divDirectoryList').html(tablestring);
}
});
</script>
<div id="divDirectoryList"></div>
处理方法是:
Public Class DepartmentListHandler
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim json = New JavaScriptSerializer()
Dim lst As New List(Of DepartmentList)
Dim cls1 As New DepartmentList(1, 0, "DEP1", "Header", 1, True, False)
lst.Add(cls1)
Dim serialisedList As String = json.Serialize(lst)
context.Response.ContentType = "application/json"
context.Response.Write(serialisedList)
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
所有可能发生的情况是,"success"分支中的警报每次都返回"undefined",并且表根本没有呈现。
有人看到我哪里错了吗?
非常感谢!
我认为您在代码中序列化的json数据格式不正确。显示为"data.msg"的json数据必须作为接收
{
msg: 'some message',
data:'some data'
};
实际上,在客户端,您将获得对象的[object object]集合,该对象包含您的对象列表。
打印console.log("数据",data);看看火狐。
您可以简单地获得
data[0].msg;
data[0].id;
或者你可以用jquery迭代java脚本对象,然后得到下面的
data.msg;
data.id
相关文章:
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- AngularJS和promise值在调用本地函数时的效果-未定义
- 为什么这个函数调用会破坏程序并导致未定义的变量
- React路由器错误-'无法调用方法'getRouteAtDepth'的未定义'
- 在不兼容的接收器上调用的方法Set.prototype.add未定义
- PhoneGap无法调用未定义的方法loadurl
- TinyMCE验证给出错误:无法调用方法'getContent'的未定义
- setInterval调用具有未定义参数的函数
- 返回List<字符串>Jquery中的from JSON调用具有未定义的长度
- REST调用总是返回未定义的(Node.JS)
- 如何处理 getJSON 调用上的“未捕获类型错误:无法设置未定义的属性'prop'”
- Javascript/jQuery - “无法调用未定义的方法'push'”,而它是定义的
- 对象不支持在调用 indexOf 定义的数组时 IE8 中未定义的此属性或方法
- 未捕获的类型错误:无法调用方法'至小写'的未定义
- *简单*解析云代码查询失败”;TypeError:无法调用方法'获取'未定义的“检索该信息”;
- 无法调用方法'应用'关于为jquery谷歌地图插件设置方向的未定义
- moment:Array.prototype.some在null或未定义时被调用
- 调用javascript函数时未定义
- 未捕获的类型错误:无法调用方法'每个'的未定义
- Alt flux action:_this.actions未定义,即使调用了this.generateActions