我一直在尋找幾個小時來解決這個問題,我找不到解決我的問題的東西。無論何時我嘗試通過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
集 - 它是。每當我不要在我的網址中使用字符串時,一切都很好。我不明白爲什麼這不起作用。它看起來不像我的編碼問題。有任何想法嗎?
將字符串括你檢查使用諸如Fiddler工具,看看是否任何差異中有串之間的查詢字符串數據的請求和整數? – DaveB 2011-04-27 22:08:36
類似的問題 - http://stackoverflow.com/questions/7830961/invalid-json-primitive-id – Vas 2013-05-08 16:28:35