从iOS应用程序调用Rails Action

时间:2014-01-17 20:27:33

标签: ios ruby-on-rails

我有一个非常简单的Rails操作,如下所示:

def create 
    @story = Story.new(story_params)
    if(@story.save)
      render :json => {:success => 'true', :message => 'Story saved successfully!'}
    else 
      render :json => {:success => 'true', :message  => 'Error creating story'}
    end 
  end 

我使用AFNetworking从我的iPhone应用程序调用上述方法:

 [self POST:@"stories/create" parameters:@{@"story[title]":story.title,@"story[abstract]":story.abstract} success:^(NSURLSessionDataTask *task, id responseObject) {

        if(success) success(task,responseObject);

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

        if(failure) failure(task,error);

    }];

我收到以下消息:

 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8ca3500 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

我用Charles查看网络流量,我得到以下信息:

story[abstract] Asd
story[title]    asdas

这是什么意思,我怎样才能上班?

更新:

我尝试了下面提到的其他方法,但仍然存在同样的问题:

 [self POST:@"stories/create" parameters:@{@"story": @{
                                                      @"title": story.title,
                                                      @"abstract": story.abstract
                                                      }
                                              }
     success:^(NSURLSessionDataTask *task, id responseObject) {

         NSLog(@"Success");

     } failure:^(NSURLSessionDataTask *task, NSError *error) {

         NSLog(@"%@",error.description);

         NSLog(@"FAILED");

     }];

1 个答案:

答案 0 :(得分:0)

不确定但对我来说这似乎不对:

@{@"story[title]":story.title,@"story[abstract]":story.abstract

怎么样?
@{@"story": @{
            @"title": story.title,
            @"abstract": story.abstract
            }
 }