使用 ASIHTTPRequest 使用 NSDictionary 来 POST 嵌套参数
尝试将嵌套参数的信息发布到 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 服务器,标签参数就无法读取(params[:asset][:tags])。
Trying to post information for nested parameters to a rails app and having some trouble.
#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 is just a NSString with comma separated values, however once arriving at the rails server the tags parameter cannot be read (params[:asset][:tags]).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将字典作为 JSON 字符串而不是字典对象传递。
您可以使用 iOS5 JSON 库来做到这一点,或者使用这个库来获得更高的兼容性:
https://github.com/stig /json-framework
我所做的是使用appendPostData,因为我非常确定设置标头(addRequestHeader)和使用addPostValue 不是兼容的函数。
这是我的代码示例:
当您使用appendPostData时,您不能使用任何addPostValue。你必须把所有的东西都放进字典里。
Try to pass you dictionary as a JSON string and not a dictionary object.
You can do that using iOS5 JSON library, or this one for more compatibility:
https://github.com/stig/json-framework
What I do is use appendPostData because I am pretty sure that setting the header (addRequestHeader) and using addPostValue are not compatible functions.
Here is an example of my code:
When you use appendPostData, you can't use any addPostValue. You have to put everything in the dictionary.
以下是 iOS 上 JSONKit 的工作代码片段。
Here is snippet of working code with JSONKit on iOS.