我对Xcode很陌生,我正在尝试发出POST请求。我在Google上搜索并找到了这个AFNetworking库。我已经阅读了一些文档,但他们都说不同的东西。所以现在我在POST请求中使用他们自己的示例,我的代码如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"fName": @"firstname", @"lName": @"lastname"};
[manager POST:@"http://00000.1111.ovh/api/Account/register" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
然后我收到了这个错误:
错误:错误Domain = com.alamofire.error.serialization.response 代码= -1011"请求失败:错误请求(400)" UserInfo = {com.alamofire.serialization.response.error.response = {URL:http://00000.1111.ovh/api/Account/register} { 状态代码:400,headers { " Content-Length的" = 159; "内容类型" =" application / json;字符集= UTF-8&#34 ;; Date =" Sun,2015年11月15日12:11:57 GMT&#34 ;; Server =" Microsoft-IIS / 8.5&#34 ;; " X供电-通过" =" ASP.NET&#34 ;; ,NSErrorFailingURLKey = http://00000.1111.ovh/api/Account/register, com.alamofire.serialization.response.error.data =< 7b226d65 73736167 65223a22 54686520 72657175 65737420 69732069 6e76616c 69642e22 2c226d6f 64656c53 74617465 223a7b22 6c6f6769 6e2e7573 65726e61 6d65223a 5b225468 65205573 6572206e 616d6520 6669656c 64206973 20726571 75697265 642e225d 2c226c6f 67696e2e 70617373 776f7264 223a5b22 54686520 50617373 776f7264 20666965 6c642069 73207265 71756972 65642e22 5d7d7d>,NSLocalizedDescription =请求失败:错误 请求(400)}
希望有人可以帮助我!
答案 0 :(得分:2)
如果您将服务器的响应转换为可以解析/检查的内容,则可以更好地诊断这些问题:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"fName": @"firstname", @"lName": @"lastname"};
[manager POST:@"http://00000.1111.ovh/api/Account/register" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error = %@", error);
if (operation.responseObject) {
NSLog(@"responseObject = %@", operation.responseObject);
} else {
NSLog(@"responseString = %@", operation.responseString);
}
}];
如果你这样做了,你会发现它回复了:
responseObject = {
message = "The request is invalid.";
modelState = {
"login.password" = (
"The Password field is required."
);
"login.username" = (
"The User name field is required."
);
};
}
显然,该请求缺少必要的身份验证详细信息。
答案 1 :(得分:1)
您的请求已正确发送,但服务器已使用HTTP代码400进行响应。请查看服务器的API文档,了解请求的外观。