2016-12-21 14 views
0

我曾試圖發佈數據到服務器的方法很多,但他們沒有工作,唯一的反應我得到的是:發佈的數據服務器和響應是:{「消息」:「發生了錯誤。」}

{「消息」:「發生錯誤」}

下面是我最近嘗試的方法。

NSString *[email protected]"2"; 
NSString *[email protected]"18"; 
NSString *[email protected]"1"; 
NSString *[email protected]"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); 
+0

聯繫服務器開發人員,並要求他們提供更好的錯誤消息。 – rmaddy

+0

@rmaddy一切從服務器端被修復 –

+2

這是服務器端錯誤,請先嚐試發送郵遞員的請求,看看它是否有錯誤,旁邊,我認爲它應該是'NSUTF8StringEncoding'而不是'NSASCIIStringEncoding' – Tj3n

回答

0

謝謝@ Tj3N,你的回答很有幫助。

只需更改編碼類型。從NSASCIIStringEncoding到NSUTF8StringEncoding。 以下是更新的代碼。

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

BASE_URL = PASS_YOUR_URL _params =參數// AS的NSMutableDictionary嘗試此。

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

這需要AFNetworking? –

+0

@Shikha你需要AFNetworking –

+2

不,你不需要'AFNetworking'。這是有用的,但不是必需的。 – rmaddy

0

您正在傳遞查詢字符串中的數據,因爲您要發佈的數據應該作爲NSDictionary對象轉換爲數據來傳遞。

相關問題