数据在Postman应用程序中运行正常,但无法在代码中运行。以下是代码
NSString* link = [NSString stringWithFormat:@"http://apifm.azurewebsites.net/token"];
AFHTTPRequestOperationManager* manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:nil];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager POST:link parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSMutableData *email = [[NSMutableData alloc] init];
NSMutableData *password = [[NSMutableData alloc] init];
NSMutableData *grantType = [[NSMutableData alloc] init];
[email appendData:[[NSString stringWithFormat:@"testman1@foodmandu.com"] dataUsingEncoding:NSUTF8StringEncoding]];
[password appendData:[[NSString stringWithFormat:@"testUserPw"] dataUsingEncoding:NSUTF8StringEncoding]];
[grantType appendData:[[NSString stringWithFormat:@"password"] dataUsingEncoding:NSUTF8StringEncoding]];
[formData appendPartWithFormData:email name:@"UserName"];
[formData appendPartWithFormData:password name:@"Password"];
[formData appendPartWithFormData:grantType name:@"grant_type"];
} success:^(AFHTTPRequestOperation* operation, id responseObject) {
NSLog(@"success");
} failure:^(AFHTTPRequestOperation* operation, NSError* error){
[API alertViewWithMessage:[[operation responseObject] objectForKey:@"error"] inController:self];
}];
以下是Postman应用程序的屏幕截图
执行代码时得到的错误在下面给出
错误错误Domain = com.alamofire.error.serialization.response Code = -1011“请求失败:错误请求(400)”UserInfo = 0x7faf3b6d6cf0 {com.alamofire.serialization.response.error.response = {URL:{ {3}}} {状态代码:400,headers { “Cache-Control”=“no-cache”; “内容长度”= 34; “Content-Type”=“application / json; charset = UTF-8”; Date =“Sun,05 Jul 2015 07:11:19 GMT”; Expires =“ - 1”; Pragma =“no-cache”; Server =“Microsoft-IIS / 8.0”; “Set-Cookie”=“ARRAffinity = ebb02fb6b319b5ae83e167620f3bc3ad6e68d31fb30aff79ef123aabd981588e; Path = /; Domain = apifm.azurewebsites.net”; “X-Powered-By”=“ASP.NET”; },NSErrorFailingURLKey = http://apifm.azurewebsites.net/token,com.alamofire.serialization.response.error.data =&lt; 7b226572 726f7222 3a22756e 73757070 6f727465 645f6772 616e745f 74797065 227d&gt;,NSLocalizedDescription =请求失败:错误请求(400)}
提醒您,Postman中的数据工作正常。
答案 0 :(得分:2)
您应该将参数作为NSDictionary传递,例如&#34; NSDictionary * parameters = @ {@&#34; UserName&#34;:@&#34; testman1@foodmandu.com" ;, @&#34;密码&#34;:@&#34; testUserPw&#34;, @&#34; grant_type&#34;:@&#34;密码&#34;}; &#34;
这可以使用以下代码实现:
function FBLogin() {
//Fb app information//
window.fbAsyncInit = function() {
Parse.FacebookUtils.init({
appId : 'xxxxxxxxx',
xfbml : true,
version : 'v2.3'
});
//rest of function here etc//
}
请尝试以上操作,并告诉我您的反馈意见。