tags:

views:

3235

answers:

3

I saw a similar question but it did not resolve my issue. I have a JSON web service in an ASMX file;

The code for the web method

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetUserRoles(string JSONUserCode)
        {
            string retRoles = string.Empty;
            List<JSONRole> roles = new List<JSONRole>();

            {... I Populate the roles here ...}

            DataContractJsonSerializer serializer = new
            DataContractJsonSerializer(roles.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, roles);
            string jsonString = Encoding.Default.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }

This correctly formats the List correctly but wraps the entire return in XML. Here is the response:

<?xml version="1.0" encoding="utf-8" ?> 
    <string xmlns="http://formshare.com/"&gt;
       [{"Name":"Accounts Payable"},{"Name":"Payroll"}]
    </string>

You can view the Response your self by clicking this link:

http://dev.formshare.gologictech.com/JSON/JSONService.asmx/GetUserRoles?JSONUserCode=1234

I need the response to be just:

[{"Name":"Accounts Payable"},{"Name":"Payroll"}]

Any ideas? Thanks for your help.

+2  A: 

The WebMethod is able to serve the same information both as XML and JSON. You need to specify what format you want (dataType) in the client, as you submit your request.

Also, you're not supposed to serialize the object to JSON manually, but rather, return roles, and it will be serialized to JSON if your client requests the data as JSON.

EDIT

jQuery example (note the dataType parameter):

$.ajax({
   type: 'GET',
   url: 'http://dev.formshare.gologictech.com/JSON/JSONService.asmx/GetUserRoles',
   contentType: 'application/json; charset=utf-8',
   dataType: 'json',
   data: '{"JSONUserCode":"1234"}',
   success: myCallback
});

It is worth mentioning that the object will not be returned in exactly the format you specified, but rather, wrapped in an object:

{ d: [ {"Name":"Accounts Payable"}, {"Name":"Payroll"} ] }

This, however, is actually quite desirable, for added security

David Hedlund
Is there anyway to specify the Datatype in a POST?
JohnnyCantCode
I'm not sure I follow... the above example is a POST.
David Hedlund
I'm sorry, I mean passing the parameter in the URL like the URL I included in my question.
JohnnyCantCode
ah, right. I updated my example above to use the url's and parameters that you're using. Using 'GET' instead of 'POST', the data passed in 'data' will be retrieved via querystring rather than form values.
David Hedlund
Since $.ajax() won't work for cross-site usage, I have to use $.getJSON(), and my service returns JSONP. I have the same problem, the json result is wrapped in xml. Since I'm using $.getJSON(), I'm not sure how to set the headers to tell the service that I want a json result, not an xml result. Any ideas how to do that, just via the URL? Is there some built-in parameter that .NET services look for in the query string?
Samuel Meacham
A: 

Make sure your service class has [ScriptService] attribute. This attribute is not added by default.

Pavel Morshenyuk
A: 

Try Jayrock for .NET, it's a neat handler for .NET JSON RPC. It'll do exactly what you need. There's a step by step how-to for ASP .NET JSON RPC here.

Ivan