2

I have created a simple .asmx web service in .NET as shown below:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(string name)
{
    var customer = new {Name = name}; 

    JavaScriptSerializer json = new JavaScriptSerializer();
    return json.Serialize(customer); 
}

When I call this service it returns XML instead of JSON result. My iOS client has a dictionary which expects JSON format.

How can I make the .NET service to return JSON format instead of XML?

The dictionary never gets populated since .Net service returns XML instead of JSON.

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"status code %d",request.responseStatusCode);

    if(request.responseStatusCode == 200) 
    {
        NSString *responseString = [request responseString];
        NSDictionary *responseDict = [responseString JSONValue];

        NSLog(@"%@",[responseDict objectForKey:@"name"]);    

    }

    NSLog(@"request finished");
}

I am also setting the content-type as shown below:

 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
    [request addRequestHeader: @"Content-Type" value: 
     @"application/json; charset=utf-8"]; 
    [request setPostValue:@"mama" forKey:@"name"];

    [request setDelegate:self]; 
    [request startAsynchronous]; 

The returned response I get is this:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"Name":"mama"}</string>
1
  • 1
    FYI, based on your stated goal, the iOS code is not relevant to the solution of this problem. Is your .NET service ASMX or WCF? Commented Aug 10, 2011 at 18:16

1 Answer 1

4

I have strictly 0 knowledge of iOS and what would the correct syntax be but in order for an ASMX web service to return JSON you need to invoke it with HTTP POST verb and set the Content-Type request header to application/json (can't see from your code doing this). It will also preprend the result with the d property, for example:

{"d": { foo: 'bar' }}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.