jQuery PUT ajax request not working

做~自己de王妃 提交于 2019-12-22 08:24:56

问题


I'm trying to save a Backbone model in couchdb so I've overridden the save method with a ajax requet to couchdb:

  $.ajax({
    type: 'PUT',
    url: 'http://127.0.0.1:5984/movies/' + this.get('id'),
    contentType: 'application/json',
    data: JSON.stringify(this.toJSON()),
    success: function() {
      console.log('asdf');
    },
    failure: function() {
     console.log('test');
    }
  });

The request is sent but when I look at the couchdb log jQuery seems to send a OPTIONS HTTP method instead of PUT:

[info] [<0.1601.0>] 127.0.0.1 - - 'OPTIONS' /movies/862 405

and couchdb sends a 405 HTTP Response code (method not allowed). Any Ideas?

Edit Here are the headers sent to CouchDB:

OPTIONS /movies/862 HTTP/1.1
Host: 127.0.0.1:5984
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost:8888
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: content-type

回答1:


There's a backbone connector for couch-db.. https://github.com/janmonschke/backbone-couchdb




回答2:


PUT isn't supported by all browsers. Also, the property for your data is "data", not "body".



来源:https://stackoverflow.com/questions/5846741/jquery-put-ajax-request-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!