我正在处理一个注册表单,我需要使用安全的 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);
}