【问题标题】:asp.net Webservice returns XML instead of JSONasp.net Web 服务返回 XML 而不是 JSON
【发布时间】:2023-04-05 20:19:01
【问题描述】:

我正在为我的 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);
}

【问题讨论】:

  • -1 你应该分享你的解决方案

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


【解决方案1】:

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

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

【讨论】:

  • 我已经尝试过了,但他仍然返回 XML。我更新了我的问题
  • 找到了解决方案。更新了我的问题。 THX 寻求帮助
猜你喜欢
  • 2012-02-26
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多