2013-04-11 99 views
1

我有一個用於iPad應用程序的API。控制器接受json並返回json。 POST請求在我本地運行服務器時起作用,而我使用ISO模擬器,但是當我部署到heroku後,當我發出POST請求時,所有作爲JSON發送的參數均爲零。Rails POST在Heroku上不起作用,但在本地工作

觀看heroku日誌提出請求時,我會得到以下內容。

at=info method=POST path=/note/update host=myapp.herokuapp.com fwd="174.50.210.40" dyno=web.1 connect=0ms service=755ms status=500 bytes=643 
2013-04-11T16:51:30.907621+00:00 app[web.1]: 
2013-04-11T16:51:30.908272+00:00 app[web.1]: Processing by MyAPIController#update_note as JSON 
2013-04-11T16:51:30.908272+00:00 app[web.1]: Parameters: {"myapi"=>{}} 
2013-04-11T16:51:30.908272+00:00 app[web.1]: Completed 500 Internal Server Error in 551ms 

我的控制器中的操作如下所示。

def update_note 

    puts "debug note update #{ params[:note] }" 

    uuid = params[:uuid] 
    user = User.find_by_uuid(uuid) 
    noteid = params[:note][:note_id] if params[:note][:note_id] 

    if !user.nil? 
     note = nil 
     note = user.notes.find(noteid) if !noteid.nil? 
     if !note.nil? 
      user.notes.find(noteid) 
      note.update_attributes!(params[:note]) 
     else 
      note = user.notes.build(params[:note]) 
     note.save 
     end 
     render :json => {:result => note} 
    else 
     render :json => {:error => "nope"} 
    end 
    end 

我擊潰文件具有以下

match '/note/update' => 'myapi#update_note' 

你是熟悉這裏的Objective-C是

NSDictionary *noteVo = [NSDictionary dictionaryWithObjectsAndKeys:note->title, @"title", note->body, @"body", note->note_id, @"note_id", nil]; 

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:[_dm getUser]->uuid, @"uuid", noteVo, @"note", true, @"remote", nil]; 

    NSError *writeError = nil; 

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&writeError]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: 
            [NSURL URLWithString:[BASE_URL stringByAppendingString:@"note/update"]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody: jsonData]; 

    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
+3

你有沒有機會做一些遷移,忘記在heroku上運行它們?如果是這樣,請記住在 – pjam 2013-04-11 17:18:55

+0

之後重新啓動實例就是這樣......我覺得自己像一個巨大的白癡。感謝您的建議。 – mattwallace 2013-04-11 20:25:54

+0

我發現它的原因是我之前犯過這個錯誤,並且不止一次:D很高興你的問題解決了! – pjam 2013-04-11 20:38:10

回答

0

我覺得你的例子不是提出請求的代碼完成(我無法看到你的Obj-C代碼中設置了「myapi」參數)

你試過嗎?使用curl在heroku上使用curl調用您的API?

我認爲這樣更容易調試。

相關問題