WCF服务:分析值时遇到意外字符
我正在尝试用客户机/服务器原理编写一个测试应用程序。服务器是一个简单的WCF服务,它通过实体模型运行CRUD->CRUDE、Read、Udate和Delete 在客户端,我有一个函数,它将json格式的数据传递给WCF服务:WCF服务:分析值时遇到意外字符,wcf,getjson,Wcf,Getjson,我正在尝试用客户机/服务器原理编写一个测试应用程序。服务器是一个简单的WCF服务,它通过实体模型运行CRUD->CRUDE、Read、Udate和Delete 在客户端,我有一个函数,它将json格式的数据传递给WCF服务: save: function (w) { var object= [{ "id": w.id, "date": w.date, "content": w.content }]; $.getJSON(
save: function (w) {
var object= [{
"id": w.id,
"date": w.date,
"content": w.content
}];
$.getJSON("http://localhost:49817/Service.svc/SaveObject/" + object).done(function (data) {
App.content.setObject(data);
});
},
public string SaveObject(string object)
{
try
{
CustomClass myCustom= (CustomClass) JsonConvert.DeserializeObject(object, typeof (JObject));
using (AppEntities entities = new AppEntities())
{
CustomClass newCustom = new CustomClass();
newCustom.id = newCustom.id;
newCustom.date= myCustom.date;
newCustom.content = myCustom.content;
entities.Wertungen.Add(newWertung);
entities.SaveChanges();
return null;
}
}
catch(Exception e)
{
throw new FaultException("An error occured during saving the new object");
}
}
以下是WCF服务:
save: function (w) {
var object= [{
"id": w.id,
"date": w.date,
"content": w.content
}];
$.getJSON("http://localhost:49817/Service.svc/SaveObject/" + object).done(function (data) {
App.content.setObject(data);
});
},
public string SaveObject(string object)
{
try
{
CustomClass myCustom= (CustomClass) JsonConvert.DeserializeObject(object, typeof (JObject));
using (AppEntities entities = new AppEntities())
{
CustomClass newCustom = new CustomClass();
newCustom.id = newCustom.id;
newCustom.date= myCustom.date;
newCustom.content = myCustom.content;
entities.Wertungen.Add(newWertung);
entities.SaveChanges();
return null;
}
}
catch(Exception e)
{
throw new FaultException("An error occured during saving the new object");
}
}
我的问题是这里的这部分
CustomClass myCustom=CustomClass JsonConvert.DeserializeObjectobject,JObject的类型
发生错误:分析值时遇到意外字符
我不知道这个错误。我已经试了几天了,直到今天才找到解决办法。
我希望你能帮助我
谢谢。执行请求时对象的内容是什么?您应该将其重命名为@object,因为object是已命名的类型。此外,这是如何编译的?它应该会给出一个错误,对象是一个关键字。您是否可以再次检查您是否正在使用fiddler或类似工具解析有效的json?我曾经遇到过这种情况,我以为我得到了json,但实际上我得到了一条格式为HTML的错误消息。@Mark:我刚刚将名称从原始名称更改为对象。这是我想做的一个简短的例子。对象的内容是请求后的对象。@Ola Ekdahl:这有点奇怪。我制作了一个函数,用于检查字符串是否为有效的json格式。我用了两个警报框。首先它意味着它是false,然后紧接着它意味着它是true……那么,如果使用调试器,SaveObjectstring对象的输入字符串是什么样子的呢?