Dav*_*Liu 0 rest ruby-on-rails nsurlrequest ruby-on-rails-3 nsjsonserialization
嗨,大家好所以我用NSJSONSerialization dataWithJSONObject:jsonDict选项:NSJSONWritingPrettyPrinted错误:零]命令写入JSON.它看起来很好,当我发布到Rails服务器时,"第一个名称"仍然存在.任何人都可以帮忙
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];
Run Code Online (Sandbox Code Playgroud)
在xcode中输出 - >>>>
{
"commit" : "Create Check i Nadtum",
"authenticity_token" : "xx",
"utf8" : "1",
"check_i_ndatum" : {
"firstname" : "dave",
"lastname" : "lu"
}
}
Run Code Online (Sandbox Code Playgroud)
输出 - >>> ON RAILS SERVER
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}
Run Code Online (Sandbox Code Playgroud)
应该看起来像这样 - >>>在铁路服务器上
{"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"}
Run Code Online (Sandbox Code Playgroud)
您尚未在帖子请求中设置内容类型,因此最终会将其解释为普通表单编码帖子.
将Content-Type标头集添加到application/json之类的应该会导致rails将请求解析为json
例如,对于上面的代码,这可能看起来像:
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1145 次 |
| 最近记录: |