2

我正在破坏骨干网中的模型,它会发送正确的 DELETE 请求。请求一切正常;我什至检查了 chrome 调试器,在网络下它说请求status 200尚未主干出于某种原因调用我的错误函数。这是代码:

destroy : function() {
    new App.Views.Notice({ message: "Removing reminder...", type: "message"});
    this.model.destroy({
        success: function(model, response){
            this.close();
            new App.Views.Notice({message: "Successfully remove reminder", type:"success"});
        },

        error: function() {
            new App.Views.Notice({message: "Could not remove reminder", type:"error"});
        }
    });
},

在我的 Rails 模型中:

def destroy
    note = Note.find_by_id(params[:id])
    note.destroy
end

(我应该在这里渲染一些 json 吗?)

这是请求信息:

DELETE /notes/41 HTTP/1.1
Host: 127.0.0.1:3000
Connection: keep-alive
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://127.0.0.1:3000
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31
Referer: http://127.0.0.1:3000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: _Notely_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTc1MTk3OGRkYzNiZGUzODY1YjYyYWM2MmNjOWZjZDViBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMXNUWGZDUkpHL1F2Um52NXB0emNsNnpmWDVVS1dmZ1RLOUtkQk9STXA2UjA9BjsARg%3D%3D--b186601186315cb3cc7aee51b92d1e2a76a8279e

回复信息:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
X-Ua-Compatible: IE=Edge
Cache-Control: no-cache
X-Request-Id: 94ab62df5f0b462901174ae329d37e1f
X-Runtime: 0.088005
Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-01-15)
Date: Thu, 28 Mar 2013 04:25:34 GMT
Content-Length: 0
Connection: Keep-Alive
Set-Cookie: _Notely_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJTA1YWViMDI2NmZjMjI3NDM4ZDcwZjBkZjhjNWVjYzFiBjsAVA%3D%3D--507388d81c70eb937fe521749b60e7f170d15671; path=/; HttpOnly

在 Chrome 调试器的响应选项卡下,它还显示“此请求没有可用的响应数据”我应该在 Rails 中发送一些东西吗?如果是这样,是什么?

4

1 回答 1

0

目前我正在使用 BackboneJS。在正常情况下,当我们在 Backbone Model 中调用 REST 方法时,它会发送 JSON 请求(您可以在服务器控制台中看到as JSON)。我更喜欢使用这样的 JSON 响应:

respond_to do |format|
  format.json {render :json => {:msg => "item deleted!"},:status => 200}
end 

我真的不知道问题出在哪里,因为它的响应状态为 200。但是为了解决这个问题,我将我的销毁代码粘贴到 Backbone(在咖啡脚本中)

model.destroy(
  success: (model, response, options) =>
    #do other stuff
  error: (model, response, options) =>
    #do stuff for error
)
于 2013-03-28T07:17:41.037 回答