views:

31

answers:

2

Dear friends ,

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

+1  A: 

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.

sTodorov
dear sTodorov I have added the source code , thank you
Mironline
+1  A: 

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).

peol