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

后端 未结 3 1329
长情又很酷
长情又很酷 2021-01-26 08:53

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.\"}

相关标签:
3条回答
  • 2021-01-26 09:08

    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) ;
                }
    }];
    
    0 讨论(0)
  • 2021-01-26 09:19

    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);
    
    0 讨论(0)
  • 2021-01-26 09:28

    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.

    0 讨论(0)
提交回复
热议问题