2011-12-02 107 views
0

我發現相當多的職位,說下面應該工作:允許通過REST Web服務通過更新服務的Rails 3

$.ajax 
({ 
    type: "PUT", 
    url: "http://localhost:3000/api/v1/markets/4/features/9?token=qqMJpyFbnqXVyPRLCwrv", 
    dataType: 'script', 
    data: "title=from form&content=this is content from the form", 
    success: function() { 
    alert("Thanks!"); 
    } 
}) 

不幸的是,服務器日誌狀態開始GET「/ API/V1/...「並返回200的狀態。 以下的工作。

curl -X PUT -d 'feature[content]=this is the content' -d 'feature[title]=new title' http://localhost:3000/api/v1/markets/4/features/9?token=qqMJpyFbnqXVyPRLCwrv 

所以我知道我在api控制器中的更新操作正常運行。我有我們希望聯合撰寫的文章,並希望數據的消費者能夠在不離開其當前位置的情況下對其進行評論。

任何幫助或鏈接,我可能沒有已經google了讚賞。

回答

0

不幸的是,你不能在瀏覽器環境中使用XHR發送PUT請求,只有GET和POST方法在瀏覽器中有效。

隨着稍加修改你的腳本將工作:這裏

$.ajax 
({ 
    type: "POST", 
    url: "http://localhost:3000/api/v1/markets/4/features/9?token=qqMJpyFbnqXVyPRLCwrv", 
    dataType: 'json', 
    data: { 
    title: 'from form', 
    content: 'this is content from the form', 
    _method: 'PUT' 
    }, 
    success: function() { 
    alert("Thanks!"); 
    } 
}) 

鍵_method:在數據對象「PUT」,這樣你可以重寫實際的HTTP方法(在這種情況下,POST)。

P.S. 我稍微更改了「data:」屬性,並分配了對象而不是字符串。這樣jQuery會照顧字符串編碼。

+0

ioseb,我試過了您的建議,但服務器返回開始GET「/ api/v1/markets/4/features/9?token = qqMJpyFbnqXVyPRLCwrv&title = from + form&content = this + is + content + from + the form&_method = PUT&_ = 1322861662848「for 127.0.0.1 at 12月2日星期五13:34:22 -0800 2011 – h8windows

+0

抱歉,我沒有注意到」腳本「被指定爲」dataType:「屬性的值。這會將POST請求轉爲GET。我在上面的例子中將它改爲「json」。 – ioseb

+0

感謝您通過那一個。現在我可以繼續讓自己陷入更深層的麻煩之中。 – h8windows