************** EDITED *******************
我对iOS比较陌生,我想使用以下代码发布帖子请求:
我收到 NSLocalizedDescription =请求失败:不可接受的内容类型:text / html,NSErrorFailingURLKey = https://xxxxxx-xxxxxx/xxxx/xxxx}},com.alamofire.serialization.response.error.response = {URL:{ {3}}}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:token1 forHTTPHeaderField:@"Authorization"];
[requestSerializer setValue:@"PP" forHTTPHeaderField:@"x-service-code"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager setRequestSerializer:requestSerializer];
NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:
token.tokenId, @"payment_method_nonce",
details.city, @"city",
details.address1, @"address1",
details.state, @"state",
details.zipcode, @"zip",
nil];
[manager POST:url parameters:params
success:^(NSURLSessionDataTask *task, id responseObject)
{
NSLog(@"SUCCEEDED");
}
failure:^(NSURLSessionDataTask *task, NSError *error)
{
loadingOverlay.hidden = YES;
NSLog(@"FAILED %@",error);
}];
我发现了问题所在:我需要将params作为原始JSON发送,而不是这样做。我如何实现这一目标?
我希望params采用原始格式,即
{ “payment_method_nonce”: “tok_xxxxxxxx”}
答案 0 :(得分:0)
解决了它,我改为使用NSMutableDictionary:
[paymentDetails setObject:token.tokenId forKey:@"payment_method_nonce"];
[paymentDetails setObject:details.address1 forKey:@"address1"];
[paymentDetails setObject:details.city forKey:@"city"];
[paymentDetails setObject:details.state forKey:@"state"];
[paymentDetails setObject:details.zipcode forKey:@"zip"];
并使用AFHTTPRequestOperationManager而不是AFHTTPSessionManager