I am writing a webservice for my IPhone application. The Webservice should return the “HelloWorld” in JSON but it returns XML. I don’t know why
I have the following Webservice
[ScriptService]
public class Service1 : System.Web.Services.WebService
{
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public string HelloWorld()
{
return "HelloWorld";
}
}
and following in Xcode
- (IBAction)parseButton:(id)sender {
NSString *urlString = @"http://217.160.223.254/Webdienst2/Service1.asmx/HelloWorld";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
if (errorReturned)
{
NSLog(@"Error");
}
else
{
NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", retVal);
}
Try setting the Content-Type request header to
application/json: