我想在javascript中调用Web服务方法。 (asp.net 3.5)
我用萤火虫追踪了结果。 结果如下:
{"d":"[{\"TI\":\"www\"},{\"TI\":\"www1\"}]"}
我认为正确的结果应该是这样的
{"d":[{\"TI\":\"www\"},{\"TI\":\"www1\"}]}
Bracket之前和之后的引用是什么?
//编辑: 在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;
谢谢你。
的mir
答案 0 :(得分:1)
引号表明这是一个字符串,所以:
var b = {"d":"[{\"TI\":\"www\"},{\"TI\":\"www1\"}]"};
b [“d”]将返回一个字符串而不是一个对象数组。 您可以使用javascript中的以下内容解决此问题:
var c = eval(b["d"]);
将字符串转换为对象数组。 或者更好的方法,发布返回此代码的代码,我们可以尝试找出它作为字符串返回的原因。
答案 1 :(得分:1)
您不需要在Web服务中手动序列化;考虑使用这样的东西:
public List<Test> Load(string e)
{
List<Test> post = new List<Test> { new Test("www"), new Test("www1") };
return post;
}
由于您使用string
作为返回对象,因此在将其序列化时(将再次)将其转换为您的返回对象。