我已经花了几个小时寻找这个问题的解决方案,但找不到任何可以解决我的问题的方法。每当我尝试通过 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/