我有一个将JSON数据发送到Ruby on Rails API的IOS应用程序。在Rails方面,如果我在本地测试,我会看到params通过就好了。很快,当我在Heroku上调用生产服务器的IOS应用程序时,params是零对象。
这是IOS代码
NSDictionary *noteVo = [NSDictionary dictionaryWithObjectsAndKeys:note->title, @"title", note->body, @"body", note->note_id, @"note_id", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:[_dm getUser]->uuid, @"uuid", noteVo, @"note", nil];
NSError *writeError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&writeError];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:[BASE_URL stringByAppendingString:@"note/update"]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
这是rails代码
def update_note
puts "debug note update #{ params[:note] }"
uuid = params[:uuid]
user = User.find_by_uuid(uuid)
noteid = params[:note][:note_id] if params[:note][:note_id]
if !user.nil?
note = nil
note = user.notes.find(noteid) if !noteid.nil?
if !note.nil?
user.notes.find(noteid)
note.update_attributes!(params[:note])
else
note = user.notes.build(params[:note])
note.save
end
render :json => {:result => note}
else
render :json => {:error => "nope"}
end
end