Jquery Ajax发布到C#WebMethod错误,带有;无效的JSON原语:System.Object。”;
早上好。我已经试着这么做了好几个星期了,但一直在兜圈子。我有一个简单的jqueryajax函数,它将数据发布到代码隐藏中的c#函数 基本上希望传递要处理的选定复选框字段的列表。 当我提交它时,我可以看到正在发出的请求和正在发送的json:Jquery Ajax发布到C#WebMethod错误,带有;无效的JSON原语:System.Object。”;,c#,jquery,json,ajax,C#,Jquery,Json,Ajax,早上好。我已经试着这么做了好几个星期了,但一直在兜圈子。我有一个简单的jqueryajax函数,它将数据发布到代码隐藏中的c#函数 基本上希望传递要处理的选定复选框字段的列表。 当我提交它时,我可以看到正在发出的请求和正在发送的json: {"item":["Section1","Section2","Section2Sub1","Section2Sub2","Section3"]} 它到达服务器端,但在尝试对其进行反序列化时,会返回以下错误消息: “无效的JSON基元:System.Obje
{"item":["Section1","Section2","Section2Sub1","Section2Sub2","Section3"]}
它到达服务器端,但在尝试对其进行反序列化时,会返回以下错误消息:
“无效的JSON基元:System.Object。”
var selection=serializer.Deserialize(item.ToString());
以下是我的代码片段:
client side
$("#Submit").click(function (e) {
var count = 0;
var countChecked = 0;
areaObj = [];
$('input[type=checkbox]').each(function () {
count++;
if (this.checked) {
//countChecked++;
//tmp = {
// "Area": $(this).attr("id")
//};
areaObj.push($(this).attr("id"));
}
});
});
function subClick(item) {
$.ajax({
type: "POST",
url: "Default.aspx/SubData",
data: JSON.stringify({ item: item }),
//data: "{'item':" + JSON.stringify(item) + "}",
dataType: "json",
contentType: "application/json; charset=utf-8"
});
};
c# Default.aspx.cs
[WebMethod]
public static string SubData(Selection item)
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
//ERROR OCCURS HERE
var selection = serializer.Deserialize(item.ToString());
return "this is successful";
}
public class Selection
{
public string Title { get; set; }
public string Description { get; set; }
public List KeyValues { get; set; }
}
public class KeyValues
{
public int AreaID { get; set; }
public string Area { get; set; }
public int Value { get; set; }
}
客户端
$(“#提交”)。单击(功能(e){
var计数=0;
var countChecked=0;
areaObj=[];
$('input[type=checkbox]')。每个(函数(){
计数++;
如果(选中此项){
//countChecked++;
//tmp={
//“区域”:$(此).attr(“id”)
//};
areaObj.push($(this.attr(“id”));
}
});
});
功能子单击(项){
$.ajax({
类型:“POST”,
url:“Default.aspx/SubData”,
数据:JSON.stringify({item:item}),
//数据:“{'item':”+JSON.stringify(item)+“}”,
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”
});
};
c#Default.aspx.cs
[网络方法]
公共静态字符串子数据(选择项)
{
var serializer=new System.Web.Script.Serialization.JavaScriptSerializer();
//这里发生错误
var selection=serializer.Deserialize(item.ToString());
返回“这是成功的”;
}
公开选课
{
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共列表键值{get;set;}
}
公共类键值
{
公共int AreaID{get;set;}
公共字符串区域{get;set;}
公共int值{get;set;}
}
有人能就哪里出了问题提供一些建议吗
public static string SubData(Selection item)
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
//ERROR OCCURS HERE
var selection = serializer.Deserialize(item.ToString());
return "this is successful";
}
这里,item
不是字符串(因此也不是要发送的JSON)。由于您正在对其调用ToString()
,因此该库可能正在尝试对类似于System.Object
的文本进行反序列化,这将失败
快速浏览一下代码,您似乎已经对
item
进行了反序列化,因此无需进一步执行任何操作使用item.ToString()
向我表明,您实际上并没有解析您认为是的json。您正在分析文本System.Object
,它是对象上.ToString()
的结果。如果在反序列化行上放置断点,您将看到item
不是一个字符串。进一步看,似乎item
已经是反序列化对象。当然是。我花了很长时间看这个,我对它有点视而不见。。。。谢谢你,罗布!你能“回答这个问题”吗?我会把它标记为解决方案。不用担心,伙计,很高兴它有用:)
public static string SubData(Selection item)
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
//ERROR OCCURS HERE
var selection = serializer.Deserialize(item.ToString());
return "this is successful";
}