This is a migrated thread and some comments may be shown as answers.

how to Deserialize getcontent()

3 Answers 101 Views
XmlHttpPanel
This is a migrated thread and some comments may be shown as answers.
feras
Top achievements
Rank 1
feras asked on 31 Mar 2010, 10:39 AM
hi,
after having some experiments with the panel i think it is really great and correct me if i am wrong but i think i can achieve the same AJAX functionality with jquery templating on the client side with the panel by doing the following technique

i create a web-service and request it by the panel and what i get is pure JASON, then i deserialize the Jason and use jquery to bind the value using $each function.

now i got tow questions, how i can deserialize the value comming from server like for example
{"d":{"__type":"user","name":"james","family":" smith"}}

i use the following code but nothing happens
 functionResponseEnding(sender, args) {
 

           
            $('#RadTextBox1_text').val(get_content().name);
           
       

        }

second question is is there a way to loop through template other than using $each function ?

thanks in advanced.

3 Answers, 1 is accepted

Sort by
0
Lini
Telerik team
answered on 02 Apr 2010, 09:31 AM
Hello,

To deserialize JSON content in the browser, you can use the ASP.NET AJAX client API - http://www.asp.net/AJAX/documentation/live/ClientReference/Sys.Serialization/JavascriptSerializerClass/JavascriptSerializerDeserializeMethod.aspx

var value = '{"d":{"__type":"user","name":"james","family":" smith"}}';
var obj = Sys.Serialization.JavaScriptSerializer.deserialize(value);
//obj.d.name is "james"

I am not sure which jQuery client templating solution you are using, but $.each() is a good solution for iterating through the elements in the response. See the following blog post for a sample implementation - http://www.west-wind.com/weblog/posts/509108.aspx


Sincerely yours,
Lini
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
feras
Top achievements
Rank 1
answered on 02 Apr 2010, 10:24 AM
hi,
thanks for the reply, but unfortunately it does not work, even with your sample

here is my code
   function Ending(sender, args) {
         //   var value = '{"d":{"__type":"user","name":"james","family":" smith"}}';
            var value = args.get_content() ;
            var obj = Sys.Serialization.JavaScriptSerializer.deserialize(value);

            alert(obj.d.name);
            $('#RadTextBox1_text').val(obj.d.name);
            $('#test').html(obj.d.name);
          //  args.set_cancel(result);

        }
      
as you see neither, the alert is showing up or textbox, div is getting any value at my end, plus i get this [object Object] inside RadXMLHTTPPANEL, which  i don't want it to be there,  any clues ?
thanks in  advanced.

0
Lini
Telerik team
answered on 02 Apr 2010, 03:06 PM
Hello,

I tried your script and it worked as expected for me. Perhaps the problem is with the value you get from the XmlHttpPanel:

var value = args.get_content();

I tried with the hardcoded value and it worked OK - alert showed james, RadTextBox1 value was set to james, content of div element with ID test was set to james.

var value = '{"d":{"__type":"user","name":"james","family":" smith"}}';


Kind regards,
Lini
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
XmlHttpPanel
Asked by
feras
Top achievements
Rank 1
Answers by
Lini
Telerik team
feras
Top achievements
Rank 1
Share this question
or