我试图启动这个操作,它甚至没有进入NSLOG。 我想它与我的stringUrl的东西,也许它不会像这样工作?AFNetworking JSON请求不起作用
这是我的代码:
NSString *stringUrl = [[NSString alloc]initWithFormat:@"http://example.com/add_user.php?username=%@&password=%@&country=%@&email=%@",[self.usernameTextField text],[self.passwordTextField text], countrySelected, [self.emailTextField text]];
NSURL *url = [NSURL URLWithString:stringUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *dict = JSON;
NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"item"]);
} failure:nil];
[operation start];
编辑: 这是我从失败中得到了块:
错误域= AFNetworkingErrorDomain代码= -1016“预期的内容类型{( “text/json”, “application/json”, “text/javascript” )},got text/html“UserInfo = 0xa2b4300 {NSLocalizedRecoverySuggestion = {”type“:”1“,”item“:”User added成功“。”} ,AFNetworkingOperationFailingURLRequestErrorKey = HTTP://EXAMPLE.com/add_user.php用户名= aaasafasfasf &密码= AAA &国家安哥拉=电子邮件& = aaaasfasfasfasfasf>,NSErrorFailingURLKey = http://EXAMPLE.com/add_user.php?username=aaasafasfasf&password=aaa&country=Angola&email=aaaasfasfasfasfasf,NSLocalizedDescription =预期的内容类型{( “文/ JSON” , “应用/ JSON”, “文/ JavaScript的” )},得到的text/html,AFNetworkingOperationFailingURLResponseErrorKey =}
使用NSLog获取stringUrl的值并粘贴到浏览器中,然后查看结果。 – edzio27
@ edzio27这就是我得到的: {“type”:“0”,“item”:“用户名已存在电子邮件已存在”} 它是我想得到但它甚至没有得到NSLog –
可能你尝试使用失败块? –