json - ASP.NET WebService test form -
is possible configure default asp.net webservice test form support json? mean test form built in in .net framework...
currently have webservice decorated [scriptservice]
, when testing using built in test form page, returns xml...i assume, happens because test page sends content-type xml default.
thanks
edit (example):
have class:
public class person { public string firstname { get; set; } public string lastname { get; set; } }
now have asp.net webservice:
[scriptservice] public class personservice : webservice { [webmethod] public person getdave() { person dave = new person(); dave.firstname = "dave"; dave.lastname = "test"; return dave; } }
when call webservice web page using jquery ajax, receive json person object {"firstname":"dave","lastname":"test"}
(not string) in javascript, when invoking webservice asp.net webservice test form (when right click on asmx file , use "preview in browser"),
it returns:
<?xml version="1.0" encoding="utf-8" ?> <person xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns="http://tempuri.org/"> <firstname>dave</firstname> <lastname>test</lastname> </person>
what want, when invoke service test page, see same output:
{"firstname":"dave","lastname":"test"}
you can use code below
[webmethod(description = "some description")] [scriptmethod(responseformat = responseformat.json)] public string functionname() { // return json data javascriptserializer js = new javascriptserializer(); string retjson = js.serialize(object); return retjson; }
and need add reference.
update
here link explain extending existing asp.net web service support json
hope helps
Comments
Post a Comment