0

i have create web service :

demo.asmx

<%@ WebService Language="C#" CodeBehind="~/App_Code/demo.cs" Class="demo" %>

demo.cs

public class demo : System.Web.Services.WebService
{


    public demo()
    {

    }

    [WebMethod]

    [ScriptMethod(UseHttpGet = true, XmlSerializeString = false, ResponseFormat = ResponseFormat.Json)]

    public string saveUserData()
    {


        Employee[] emps = new Employee[] {  
            new Employee()  
            {  
                Id=1,  
                Name="xyz" 
            },  
            new Employee()  
            {  
                Id=2,  
                Name="abc" 
            }  
        };

        return new JavaScriptSerializer().Serialize(emps);

    }

}

Now when i run this then it gives me below data:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
      <string>[{"Id":1,"Name":"xyz"},{"Id":2,"Name":"abc"}]</string>

so i have check the console and it gives me the

Cache-Control → private, max-age=0
Content-Encoding → gzip
Content-Length → 232
Content-Type → text/xml; charset=utf-8
Date → Sat, 05 Mar 2016 06:33:53 GMT
Server → Microsoft-IIS/8.0
Vary → Accept-Encoding
X-AspNet-Version → 4.0.30319
X-Powered-By → ASP.NET
X-SourceFiles → =?UTF-8?B?RDpcRGVtb193ZWJz

Its gives content type is text/xml even i have define the Json response format.

How can i get only json response like below ?

[{"Id":1,"Name":"xyz"},{"Id":2,"Name":"abc"}]
  • You don't manually serialize to JSON in web services. You return the object itself, and the service serializes it to the correct type. Other than that, - – GSerg Mar 05 '16 at 06:56
  • Possible duplicate of [Get JSON data with jQuery from a .NET service: confused with ajax setup](http://stackoverflow.com/questions/5690882/get-json-data-with-jquery-from-a-net-service-confused-with-ajax-setup) – GSerg Mar 05 '16 at 06:57
  • @GSerg i am not using any `ajax` in aspx page. this applicaiton doesnt content aspx pages . i am just creating webservices only. –  Mar 05 '16 at 07:01
  • It does not matter if you are using ajax or not. Do you provide the [correct content-type](http://stackoverflow.com/a/6023810/11683)? – GSerg Mar 05 '16 at 07:03
  • @GSerg I have that confusion only. how to pass content type ? because i am just hit the url like - `http://localhost:2079/demo.asmx/saveUserData` . so where i have to define the `content type` ? –  Mar 05 '16 at 07:09
  • @GSerg can you explain how to pass `content type` ? –  Mar 05 '16 at 07:18
  • You set content type in whatever thing you are using to fetch data. Most commonly it's ajax. The browser's url bar is not a proper testing tool for web services. You should have a test client for your web service, e.g. as a static html page with jquery that makes ajax calls. – GSerg Mar 05 '16 at 07:37

1 Answers1

0

you should use the following, Don't serialize manually, just do the following

[WebService(Namespace = "http://example.com/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]System.Web.Services.WebService
[ScriptService]
public class services :WebService  
{    
    [WebMethod(CacheDuration = 60)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<TestObject> GetObjectCollection()
    {
           return YourService.GetObjectCollection().ToList();
    }
}

Refrences: getting-json-data-using-an-asmx-web-service or here

Community
  • 1
  • 1
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
  • yes its working with [link](http://stackoverflow.com/questions/19563641/how-to-get-json-response-from-a-3-5-asmx-web-service) –  Mar 05 '16 at 07:16
  • @deepak I have downvoted that solution and left a comment why. – GSerg Mar 05 '16 at 07:34