从iOS向rails服务器发送带有JSON的HTTP post
通过将内容类型设置为application/json和以下json代码,我成功地使用HTTP客户端发布了一篇文章:从iOS向rails服务器发送带有JSON的HTTP post,ios,ruby-on-rails,json,Ios,Ruby On Rails,Json,通过将内容类型设置为application/json和以下json代码,我成功地使用HTTP客户端发布了一篇文章: { "order": { "name": "Tia Carter", "location": "Corams", "phone_number": "707", "food": "Bobcat Burger" } } 代码工作正常,订单已在数据库中注册。我正试图将其应用到我的iOS应用程序中,但json
{
"order": {
"name": "Tia Carter",
"location": "Corams",
"phone_number": "707",
"food": "Bobcat Burger"
}
}
代码工作正常,订单已在数据库中注册。我正试图将其应用到我的iOS应用程序中,但json中的冒号一直存在语法错误。这是objective-c代码:
NSURL *nsURL = [[NSURL alloc] initWithString:@"http://0.0.0.0:3000/orders.json"];
NSMutableURLRequest *nsMutableURLRequest = [[NSMutableURLRequest alloc] initWithURL:nsURL];
// Set the request's content type to application/x-www-form-urlencoded
[nsMutableURLRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// Set HTTP method to POST
[nsMutableURLRequest setHTTPMethod:@"POST"];
// Set up the parameters to send.
NSString *paramDataString = [NSString stringWithFormat:@
"{ "order": {"%@:" ="%@","%@:"="%@","%@:"="%@","%@:"="%@"}}", @"name", _name.text, @"location", _location.text, @"phone_number", _phoneNumber.text, @"food", _order.text];
// Encode the parameters to default for NSMutableURLRequest.
NSData *paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding];
// Set the NSMutableURLRequest body data.
[nsMutableURLRequest setHTTPBody: paramData];
// Create NSURLConnection and start the request.
NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]initWithRequest:nsMutableURLRequest delegate:self];
如果有任何想法或指导,我将不胜感激。谢谢。我相信您有两个问题:
- 您没有逃脱配额(将\放在所有配额之前)
- 您不需要在参数中输入文本“name”、“location”等(这本身不是问题,只是样式问题)