【发布时间】:2009-07-18 14:27:09
【问题描述】:
我正在使用完全 ajaxified 的 asp.net 页面(使用 jquery lib)并调用另一个 asp.net 回调页面来获取/发布数据到服务器。 我的页面的一些用户在序列化 json 对象时遇到以下错误
反序列化时出错 类型的对象...对象类型...包含无效 utf8 字节
$.ajax({
type: "POST",
async: false,
url: 'AjaxCallbacks.aspx?Action=' + actionCode,
data: {
objectToSerialize: JSON.stringify(obj, null, 2)
},
dataType: "json",
success: function(operationResult) {
//handle success
},
error: function(xhttp, textStatus, errorThrown) {
//handle error
}
});
为了解决这个问题,我添加了“contentType”选项...
$.ajax({
type: "POST",
async: false,
url: 'AjaxCallbacks.aspx?Action=' + actionCode,
data: {
objectToSerialize: JSON.stringify(obj, null, 2)
},
contentType: 'application/json; charset=utf-8', //<-- added to deal with deserializing error
dataType: "json",
success: function(operationResult) {
//handle success
},
error: function(xhttp, textStatus, errorThrown) {
//handle error
}
});
但现在我无法像以前那样在服务器端读取此对象:
string objectJson = Request.Params["objectToSerialize"].ToString();
我收到以下错误:“对象引用未设置为对象的实例。”
有什么想法吗?
【问题讨论】:
-
你能检查一下传递给服务器的 post 参数是什么吗?使用 Firebug 进行检查。
-
这是我在帖子标签 (firebug) objectToSerialize=%7B%0A++%22Id%22%3A+%22846%22%2C%0A++%22FolderId%22%3A+% 中找到的示例22405%22%2C%0A++%22Positio%22%3A+%22%22%0A%7D 参数选项卡(萤火虫)仅包含我认为我正在发布的操作代码,并且不应该能够通过参数列表访问此 json 对象在服务器上,它应该在表单集合中,尚不确定如何获取它..
-
我想我应该阅读 Request.InputStream ...?
标签: jquery ajax json serialization