Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从iOS向rails服务器发送带有JSON的HTTP post_Ios_Ruby On Rails_Json - Fatal编程技术网

从iOS向rails服务器发送带有JSON的HTTP post

从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

通过将内容类型设置为application/json和以下json代码,我成功地使用HTTP客户端发布了一篇文章:

{
    "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”等(这本身不是问题,只是样式问题)
此外,我建议您使用NSDictionary,并在需要时将其转换为JSON(这将为您节省大量的精力,避免出现未切换配额、缺少括号等情况)

查看如何将NSDictionary转换为JSON的问题: