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

倾然丶 夕夏残阳落幕 提交于 2019-12-02 17:38:48

问题


I have tried many methods of posting data to server but none of them worked and only response am getting is:

{"Message":"An error has occurred."}

Below is the method I've tried recently.

NSString *cust_id=@"2";
NSString *Prod_ID=@"18";
NSString *Vend_ID=@"1";
NSString *Quant=@"2";
NSString *post =[NSString stringWithFormat:@"customerid=%@&productid=%@&vendorid=%@&quantity=%@",cust_id,Prod_ID,Vend_ID,Quant];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSURL *url = [NSURL URLWithString:@"http://dealnxt.com/api/addtocart"];
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);

回答1:


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.




回答2:


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);



回答3:


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) ;
            }
}];


来源:https://stackoverflow.com/questions/41255201/posting-data-to-server-and-response-is-messagean-error-has-occurred

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!