json - What am I doing wrong with this Service Stack Web Service or jQuery call? -


i able service stack's hello world example working, i'm trying expand little return custom site object. made simple test html file uses jquery pull result back, i'm not getting site object returned (i think).

here's web service:

using funq; using servicestack.serviceinterface; using servicestack.webhost.endpoints; using system;  namespace my.webservice {      public class siterepository     {      }      public class site     {         public string name { get; set; }         public string uri { get; set; } //switch real uri class if find useful     }      public class siteservice : service //: restservicebase<site>     {         public siterepository repository { get; set; } //injected ioc          public object get(site request)         {             //return new site { name = "google", uri = "http://www.google.com" };             return new siteresponse {result = new site {name = "google", uri = "http://www.google.com"}};         }     }      public class siteresponse     {         public site result { get; set; }     }      public class siteapphost : apphostbase     {          public siteapphost()             : base("site web services", typeof(siteservice).assembly)         {         }          public override void configure(container container)         {             container.register(new siterepository());              routes                 .add<site>("/site")                 .add<site>("/site/{id}/");         }     }      public class global : system.web.httpapplication     {           protected void application_start(object sender, eventargs e)         {             new siteapphost().init();         }          protected void session_start(object sender, eventargs e)         {          }          protected void application_beginrequest(object sender, eventargs e)         {          }          protected void application_authenticaterequest(object sender, eventargs e)         {          }          protected void application_error(object sender, eventargs e)         {          }          protected void session_end(object sender, eventargs e)         {          }          protected void application_end(object sender, eventargs e)         {          }     } } 

here's test html file using jquery. added ?callback=? because i'm running both web service , making call same machine.

 <html>                                                                    <head>                                                                    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>            <script type="text/javascript">                                              // add our javascript code here      $(document).ready(function() {         // stuff when dom ready         alert("hello world!");         //the ?callback=? part testing on same server web service         $.getjson("http://localhost:61549/site?callback=?", function(sitesreturned) {             alert(sitesreturned);   // alert box contains:   [object object]             alert(sitesreturned.name);  //alert box contains:  undefined             alert(sitesreturned.length == 1) //alert box contains:  false             var parsed = json.parse(sitesreturned); //this fails silently             alert(parsed.name); // alert box not load         });          alert("goodbye world!");     });                                      </script>                                                                 </head>                                                                   <body>                                                                      <!-- add our html content here -->                                            hello  </body>                                                                   </html> 

a few notes...

  • but i'm not getting site object returned (i think)

$.getjson have response looks

{"result":{"name":"google","uri":"http://www.google.com"}} name/uri properties inside result property.

alert(sitesreturned.result);   // still contain [object object] alert(sitesreturned.result.name);  //should contain google alert(sitesreturned.result.uri.length == 1) //contains false since 21 != 1 
  • i'm running both web service , making call same machine.

not sure mean this. if html file contains jquery code being served http://localhost:61549 don't need use jsonp.

  • var parsed = json.parse(sitesreturned); //this fails silently

the sitesreturned parameter parsed javascript object line fails since trying parse object , not string. see documentation here. also, didn't see reference or <script> tag i'm assuming using douglas crockford's json library json.parse().

from documentation:

"the success callback passed returned data, typically javascript object or array defined json structure , parsed using $.parsejson() method. passed text status of response."


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -