2011-04-27 63 views
2

我一直在寻找几个小时来解决这个问题,我找不到解决我的问题的东西。无论何时我尝试通过HTTP Get将包含字符串值(NSString)的URL发送到我的服务器,我都会收到Invalid JSON Primitive将JSON发布到Web服务“无效的JSON原语”

我的代码:

Client Side 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]; 
} 

Server Side C#

这是行不通的。无论何时我有一个字符串作为参数,我都会收到消息,例如:

Invalid JSON Primitive

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

然而,这工作正常。 (显然,如果我改变我的测试1的parm为整数)

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

一切我已阅读指向我,确保我有application/json集 - 它是。每当我不要在我的网址中使用字符串时,一切都很好。我不明白为什么这不起作用。它看起来不像我的编码问题。有任何想法吗?

+0

将字符串括你检查使用诸如Fiddler工具,看看是否任何差异中有串之间的查询字符串数据的请求和整数? – DaveB 2011-04-27 22:08:36

+0

类似的问题 - http://stackoverflow.com/questions/7830961/invalid-json-primitive-id – Vas 2013-05-08 16:28:35

回答

3

你需要在双引号内,即该URL应该是

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

双引号不起作用,但是,单引号。 – 2011-04-27 22:53:23