Posting data to server and response is :{“Message”:“An error has occurred.”}

孤街浪徒 提交于 2019-12-02 08:35:15

You are passing the data in query string where as the data which you want to post should be passed as NSDictionary object converted to Data.

Thank You @Tj3N, your answer was helpful.

Just needed to change encoding type. From NSASCIIStringEncoding to NSUTF8StringEncoding. Below is the Updated code.

NSString *post =[NSString stringWithFormat:@"customerid=%@&productid=%@&vendorid=%@&quantity=%@",cid,PID,VID,QQ];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSURL *url = [NSURL URLWithString:@"URL"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:postData];
NSURLResponse *response;
NSError *error;
NSData *urlData=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"str:%@",str);

BASE_URL = PASS_YOUR_URL _params = parameters // AS NSMutableDictionary Try this.

 NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
 [params setValue:YOUR_VALUE forKey:@"cust_id"]; // here pass your field that you want to pass as parameter. 
 [params setValue:YOUR_VALUE forKey:@"Prod_ID"];

 NSString *url = [BASE_URL stringByAppendingString:_action];

 AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
 manager.requestSerializer = [AFHTTPRequestSerializer serializer];
 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
[manager POST:url parameters:_params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"response = %@", responseObject);
        if( _success )
        {
            _success( responseObject ) ;
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"error = %@", error);
            if( _failure )
            {
                _failure( error) ;
            }
}];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!