2013-03-17 44 views
0

我嘗試使用下面的代碼來檢查數據保存:阿賈克斯只承認JSON,如果我創建它手動

$parent.find('button[name=save]').click(function() { 
    $.ajax({ 
    url: "/help.json", 
    data: {"data": handsontable.getData()}, //returns all cells' data 
    dataType: 'json', 
    type: 'POST', 
    success: function (res) { 
     if (res.result === 'ok') { 
     $console.text('Data saved'); 
     } 
     else { 
     $console.text('Save error'); 
     } 
    }, 
    error: function() { 
     $console.text('Save error, not working'); 
    } 
    }); 
}); 

我使用Rails作爲服務器端代碼。如果我手動創建/help.json.erb並添加下面的頁面

{ 
    "result": "ok" 
} 

的,我得到一個「數據保存」的消息時,我嘗試了Ajax代碼。

但是,如果我嘗試使用ruby創建/help.json文件,則會出現「保存錯誤,無法正常工作」錯誤。我的Ruby代碼來創建/help.json文件是在我的控制器如下:

class StaticPagesController < ApplicationController 
respond_to :html, :json 

    def home 
    end 

    def help 
     the_hash = {"result"=>"ok"} 
     respond_with(the_hash) 
    end 
end 

上面的代碼生成與

{"result": "ok"} 

一個/help.json文件,但我不明白的'已保存數據'消息

當我手動創建json文件時,它的工作原理是什麼,但是如果我嘗試使用ruby,則可能不會?

我的路線文件的相關部分是:

root to: 'static_pages#home' 
    match '/help', to: 'static_pages#help' 

在進一步的檢查,我在500收到錯誤消息(內部服務器錯誤)

+1

你可以發佈routes.rb(至少有關該控制器的相關行)和更多的控制器代碼? – fmendez 2013-03-17 13:01:09

+0

你可以試試'curl -v'到你的終點,看看返回的頭文件是否有任何不同? – 2013-03-17 13:01:56

+0

@fmendez我添加了更多來自我的控制器和路由文件的代碼 – Zakoff 2013-03-17 13:07:57

回答