-2

我正在为我的 iPhone 应用程序编写一个网络服务。Web 服务应该以 JSON 格式返回“HelloWorld”,但它返回 XML。我不知道为什么

我有以下网络服务

    [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);
}
4

1 回答 1

0

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

[request setHTTPMethod: @"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *errorReturned = nil;
于 2012-05-30T07:34:17.633 回答