asp.net Web 服务和 json 结果中的额外 qouatation !
我想在 javascript 中调用 Web 服务方法。 (asp.net 3.5)
我已经用 firebug 跟踪了结果。 这是结果:
{"d":"[{\"TI\":\"www\"},{\"TI\":\"www1\"}]"}
我认为正确的结果应该是这样的
{"d":[{\"TI\":\"www\"},{\"TI\":\"www1\"}]}
Bracket 之前和之后的 quotation 是什么?
// 编辑过的: 在 webserivce:
public class Test
{
public Test(string t){T1 = t;}
public string T1 { set; get; }
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
public string Load(string e)
{
List<Test> post = new List<Test> { new Test("www"), new Test("www1") };
return JsonConvert.SerializeObject(post);
}
和 js 文件中:
var store = new Ext.data.JsonStore({
proxy: new Ext.data.HttpProxy({
url: '/core/webservice/service.asmx/Load',
method: 'GET',
headers: { 'Content-type': 'application/json' }
}),
root: 'd',
id: 'Id',
fields: ['TI']
});
store.load({ params: { e: ''} });
return;
谢谢。
米尔
I want to call a web service method in javascript. (asp.net 3.5)
I have traced the result with firebug .
here is the result:
{"d":"[{\"TI\":\"www\"},{\"TI\":\"www1\"}]"}
I think the correct result should like this
{"d":[{\"TI\":\"www\"},{\"TI\":\"www1\"}]}
what is the quotation before and after the Bracket ?
// edited :
in webserivce:
public class Test
{
public Test(string t){T1 = t;}
public string T1 { set; get; }
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
public string Load(string e)
{
List<Test> post = new List<Test> { new Test("www"), new Test("www1") };
return JsonConvert.SerializeObject(post);
}
and in js file :
var store = new Ext.data.JsonStore({
proxy: new Ext.data.HttpProxy({
url: '/core/webservice/service.asmx/Load',
method: 'GET',
headers: { 'Content-type': 'application/json' }
}),
root: 'd',
id: 'Id',
fields: ['TI']
});
store.load({ params: { e: ''} });
return;
thank you .
mir
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要在 Web 服务中手动序列化;考虑使用类似这样的东西:
由于您使用
string
作为返回对象,它会在序列化它时将其转换为您的返回对象(再次)。You shouldn't need to serialize manually in the web service; consider using something like this instead:
Since you're using
string
as your return object, it will convert it as that for you when serializing it (once again).引号表明这是一个字符串,因此 :
b["d"] 将返回一个字符串而不是对象数组。
您可以使用 javascript: 中的以下代码来解决此问题:
这会将字符串转换为对象数组。
或者更好的方法是,发布返回它的代码,我们可以尝试找出为什么它作为字符串返回。
The quotation indicates that this is a string, so :
b["d"] will return a string instead of array of objects.
You can either go around this with the following in javascript:
which will turn the string into an array of objects.
Or the better way, post the code that is returning this and we can try to figure out why it is returned as a string.