将数据发布到服务器和响应是:{" Message":“发生了错误。”} [英] Posting data to server and response is :{"Message":"An error has occurred."}

查看:134
本文介绍了将数据发布到服务器和响应是:{" Message":“发生了错误。”}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多方法将数据发布到服务器,但没有一种方法可以工作,只有响应才是:

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


推荐答案

谢谢@ Tj3N,您的回答很有帮助。

Thank You @Tj3N, your answer was helpful.

只需更改编码类型即可。从NSASCIIStringEncoding到NSUTF8StringEncoding。
以下是更新后的代码。

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

这篇关于将数据发布到服务器和响应是:{" Message":“发生了错误。”}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆