sendSynchronousRequest在JSON响应之前注册用户

时间:2013-08-28 23:31:40

标签: ios objective-c json post nsdata

我正在处理注册表单,我需要使用安全的POST请求。

如果我只使用NSJSONSerialization变量data,那么效果很好。没有错误。

问题在于,当我致电sendSynchronousRequest并尝试在变量NSJSONSerialization上致电secureData时,registrationError说“已经存在该电子邮件的用户”

我该如何避免这种情况?

这是我的代码:

NSData *data = [NSData dataWithContentsOfURL:registrationURL];

NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:registrationURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];

[postRequest setHTTPMethod:@"POST"];
[postRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[postRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[postRequest setValue:[NSString stringWithFormat:@"%d", data.length] forHTTPHeaderField:@"Content-Length"];
[postRequest setHTTPBody:data];

NSURLResponse *response;
NSError *error;
NSData *secureData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];

NSLog (@"%@", error);

if (secureData != nil)
{
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:secureData options:kNilOptions error:&error];
    NSString *registrationError = [json objectForKey:@"error"];

    NSLog(@"%@", registrationError);
}

编辑:以下是没有POST的代码:

NSData *data = [NSData dataWithContentsOfURL:registrationURL];

if (data != nil)
{
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSString *registrationError = [json objectForKey:@"error"];

    NSLog(@"%@", registrationError);
}

1 个答案:

答案 0 :(得分:0)

这是您正在使用的服务器的错误消息。当您使用相同的电子邮件地址对其进行两次测试时,用户在第一次运行中注册,并且在以下尝试中已存在。