尝试将嵌套参数的信息发布到rails应用程序并遇到一些麻烦。
#pragma mark - Begin Network Operations
- (void)beginNetworkOperation {
NSURL *requestURL = [NSURL URLWithString:[self retrieveURL]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:requestURL];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
[request setShouldContinueWhenAppEntersBackground:YES];
#endif
[request setRequestMethod:@"PUT"];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request addPostValue:strClientId forKey:@"client_id"];
[request addPostValue:strAccessToken forKey:@"access_token"];
NSDictionary *assetDictionary = [NSDictionary dictionaryWithObject:self.tags forKey:@"tags"];
[request addPostValue:assetDictionary forKey:@"asset"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request startSynchronous];
}
self.tags只是一个带有逗号分隔值的NSString,但是一旦到达rails服务器,就无法读取tags参数(params [:asset] [:tags])。
答案 0 :(得分:2)
尝试将字典作为JSON字符串传递,而不是字典对象。
你可以使用iOS5 JSON库或者这个来实现更好的兼容性:
https://github.com/stig/json-framework
我所做的是使用appendPostData,因为我很确定设置标头(addRequestHeader)和使用addPostValue是不兼容的功能。 以下是我的代码示例:
ASIFormDataRequest *request;
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request appendPostData:[[SBJsonWriter new] dataWithObject:myDictionaryToPassAsAnArgument]];
使用appendPostData时,不能使用任何addPostValue。你必须将所有内容都放在字典中。
答案 1 :(得分:2)
以下是iOS上JSONKit的工作代码片段。
[request addRequestHeader:@"Content-Type" value:@"application/json"];
NSMutableDictionary *requestDict = [[NSMutableDictionary alloc] init];
[requestDict setObject:@"iSteve" forKey:@"UserId"];
[requestDict setObject:@"1" forKey:@"CompanyCode"];
[requestDict setObject:@"IN" forKey:@"LineOfBusiness"];
[requestDict setObject:@"M" forKey:@"LineOfBusinessClassification"];
[requestDict setObject:pricingVariablesListString forKey:@"CarQuoteString"];
[request appendPostData:[requestDict JSONData]];