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

后端 未结 3 1381
长情又很酷
长情又很酷 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: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);
    

提交回复
热议问题