asp.net - 将 JSON 发布到 Web 服务 "Invalid JSON Primitive"

标签 asp.net objective-c cocoa-touch web-services

我已经花了几个小时寻找这个问题的解决方案,但找不到任何可以解决我的问题的方法。每当我尝试通过 HTTP Get 将包含字符串值 (NSString) 的 URL 发送到我的服务器时,我都会收到 Invalid JSON Primitive

我的代码:

客户端 Obj-C

- (void) createRequest: (NSString*)urlFormatted {

    NSURL *url = [NSURL URLWithString: urlFormatted];
    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL: url];

    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    _webData = [[NSMutableData data] retain];
    _connection = [[NSURLConnection alloc] initWithRequest:request  delegate:self];
}


-(void) authenticate:(NSString*) userName password:(NSString*)password
{
    NSString*url = @"http://service.example.com/service.asmx/Test?test1=hi";
    [self createRequest:url];
    [url release];
}

服务器端 C#

这不起作用。每当我有一个字符串作为参数时,我都会收到消息,

无效的 JSON 基元 例如

[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string Test(string test1)
{
   return "Ok.";  // For testing purposes, just return a string.
}

然而,这工作得很好。 (显然,如果我将 test1 参数更改为整数)

[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string Test(int test1)
{
   return "Ok.";  // For testing purposes, just return a string.
}

我读到的所有内容都表明我要确保我已经设置了 application/json - 确实如此。每当我在网址中不使用字符串时,一切都很好。我不明白为什么这不起作用。对我来说,它看起来不像编码问题。有什么想法吗?

最佳答案

您需要将字符串括在双引号内,即 URL 应该是

http://service.example.com/service.asmx/Test?test1="hi"

关于asp.net - 将 JSON 发布到 Web 服务 "Invalid JSON Primitive",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5810999/

相关文章:

jquery - 动画 GIF 在 IE 中停止但在 FF 和 Chrome 中工作

javascript - 按钮的 onclick 事件未调用 Javascript 方法

ios - 在 iOS 中合并/重叠捕获的视频

ios - 帖子未按顺序显示

ios - swift -线程 1 : Signal SIGABRT

asp.net - 我如何让中继器的Itemcommand事件不执行整页回发?

ios - 无法关闭 JSONkit 的 ARC

iphone - 将图像添加到 UITextView

ios - 以前的 NSDictionary 现在到 JSON 数组

c# - Linq 到实体 (EF) : How to get the value of a FK without doing the join