0

안녕하세요 여러분, NSJSONSerialization dataWithJSONObject를 사용하고 있습니다. jsonDict options : NSJSONWritingPrettyPrinted error : nil] json을 작성하는 명령입니다. 그것은 괜찮아 보이는 레일 서버에 게시 할 때 \ "이름"아직 거기에 있습니다. 수있는 한 도움NSJSONSerialization, IOS 끝에서 괜찮아 보이지만 레일 3 서버에서 도주하지 않았다.

NSArray *objects = [NSArray arrayWithObjects:@"firstname", @"lastname",nil]; 
NSArray *keys = [NSArray arrayWithObjects:@"dave", @"lu", nil]; 
NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:keys forKeys:objects]; 


NSArray *objects2 = [NSArray arrayWithObjects:@"utf8", 
           @"authenticity_token",@"check_i_ndatum", @"commit",nil]; 
NSArray *keys2 = [NSArray arrayWithObjects:@"1", @"xx",questionDict,@"Create Check i 
                      Nadtum", nil]; 
NSDictionary *jsonDict = [NSDictionary dictionaryWithObjects:keys2 forKeys:objects2]; 

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict 
              options:NSJSONWritingPrettyPrinted error:nil]; 


NSURL * url =[NSURL URLWithString:@"http://192.168.1.105:3000/check_i_ndata"]; 
//the request 

NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url  
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; 
//bind req w/ json 
[request setHTTPMethod:@"POST"]; 
//[request setValue:@"check_i_ndata" forHTTPHeaderField:@"Accept"]; 
[request setValue:[NSString stringWithFormat:@"%d",[jsonRequestData length]] 
forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:jsonRequestData]; 

NSURLResponse * response = nil; 
NSError *error =nil; 
NSData *reslut =[NSURLConnection sendSynchronousRequest:request 
returningResponse:&response error:&error]; 

OUTPUT - >>>> 엑스 코드는

{ 
    "commit" : "Create Check i Nadtum", 
    "authenticity_token" : "xx", 
    "utf8" : "1", 
    "check_i_ndatum" : { 
    "firstname" : "dave", 
    "lastname" : "lu" 
    } 
} 

출력 - >>> ON은 서버를 레일

Started POST "/check_i_ndata" for 192.168.1.100 at 2012-07-29 10:25:37 -0700 
Processing by CheckINdataController#create as */* 
Parameters: {"{\n \"commit\" : \"Create Check i Nadtum\",\n \"authenticity_token\" : 
\"xx\",\n \"utf8\" : \"1\",\n\"check_i_ndatum\" : {\n \"firstname\" : \"dave\",\n 
\"lastname\" : \"lu\"\n }\n}"=>nil} 

다음과 같아야합니다 - >>> ON RAILS SERVER

{"utf8"=>"✓", "authenticity_token"=>"XtLdI2lWSfO6OL8OwZ9Kai+Cm7hFe16EH50zPfheGbs=", 
"check_i_ndatum"=>{"firstname"=>"sdf", "lastname"=>"asdf", 
"medicalID"=>"adsf", "checkedInDate(1i)"=>"2012", 
"checkedInDate(2i)"=>"7", "checkedInDate(3i)"=>"29", 
"checkinTime(1i)"=>"2012", "checkinTime(2i)"=>"7", 
"checkinTime(3i)"=>"29", "checkinTime(4i)"=>"08", 
"checkinTime(5i)"=>"40"}, "commit"=>"Create Check i ndatum"} 

답변

5

y에 콘텐츠 유형을 설정하지 않았습니다. 우리의 게시물 요청 그래서 그것은 정상적인 형태로 인코딩 된 게시물로 해석지고 끝납니다.

응용 프로그램 같은 설정 Content-Type 헤더를 추가/JSON은

이 위의 코드에 대한 예를 들어 보일 수 있습니다 JSON으로 요청을 구문 분석 레일 발생합니다 같은 :

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];