-1

I have a very simple Rails action as shown below:

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 

I invoke the above method from my iPhone app using AFNetworking:

 [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);

    }];

I get the following message:

 "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.}

I used Charles to see the network traffic and I get the following:

story[abstract] Asd
story[title]    asdas

What does it mean and how can I get to work?

UPDATE:

I tried the other approach mentioned below but still the same issues:

 [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");

     }];
john doe
  • 9,220
  • 23
  • 91
  • 167
  • This question might help explain: http://stackoverflow.com/questions/14171111/cocoa-error-3840-using-json-ios – Jim Jan 17 '14 at 20:30
  • @Jim It is not the response that is causing the problem. It is the request. The data is not even reaching the rails action. – john doe Jan 17 '14 at 20:33

1 Answers1

0

Not sure but this seems wrong for me :

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

What about

@{@"story": @{
            @"title": story.title,
            @"abstract": story.abstract
            }
 }

?

rmonjo
  • 2,675
  • 5
  • 30
  • 37