asp.net Webservice返回XML而不是JSON

时间:2012-05-30 07:31:37

标签: iphone asp.net ios xcode web-services

我正在为我的iPhone应用程序编写Web服务。 Web服务应返回JSON中的“HelloWorld”,但它返回XML。我不知道为什么

我有以下Webservice

    [ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public string HelloWorld()
        {
            return "HelloWorld";
        }
    }

并在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);
}

1 个答案:

答案 0 :(得分:0)

尝试将Content-Type请求标头设置为application/json

[request setHTTPMethod: @"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *errorReturned = nil;