开发者

asp.net web service and extra qouatation in json result !

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, UseHttpG开发者_如何学运维et = 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


You shouldn't need to serialize manually in the web service; consider using something like this instead:

public List<Test> Load(string e)
{
    List<Test> post = new List<Test> { new Test("www"), new Test("www1") };
    return post;
}

Since you're using string as your return object, it will convert it as that for you when serializing it (once again).


The quotation indicates that this is a string, so :

var b = {"d":"[{\"TI\":\"www\"},{\"TI\":\"www1\"}]"};

b["d"] will return a string instead of array of objects. You can either go around this with the following in javascript:

var c = eval(b["d"]);

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜