我想为后端服务器创建用户,我需要输入以下信息来注册用户。
{
"user": {
"email": "user@example.com",
"password": "secret",
"password_confirmation": "secret",
"seed_recipes": true //optional
}
我为注册用户编写了以下代码:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"user[email]":email, @"user[password]":password, @"user[password_confirmation]":password};
[manager POST:@"http://domain.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
我遇到了以下错误。有没有人知道原因? (JSON文本不以数组或对象和选项开头,以允许未设置片段。)UserInfo = 0x7fbe935cb360 {NSDebugDescription = JSON文本不以数组或对象开头,并且选项允许未设置片段。,NSUnderlyingError = 0x7fbe935c54b0"请求失败:未经授权(401)"}
答案 0 :(得分:1)
NSDictionary *users=[[NSDictionary alloc] initWithObjects:@{@"email":email, @"password":password, @"password_confirmation":password}];
NSDictionary *parameters = [[NSDictionary alloc] initWithObjects:@{@"user":users}];
试试这个
答案 1 :(得分:0)
可能您的服务器不会以纯HTML格式响应JSON格式。
尝试添加:
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
如果这没有帮助,请确保您的服务器以JSON格式响应您正在尝试的POST。