我遇到骨干PUT请求返回405的问题(方法不允许)。发生这种情况是因为代码中的model.save()没有使用模型的ID发送到模型URL。
这是PUT。
Remote Address:::1:670
Request URL:http://localhost:670/api/contacts
Request Method:PUT
Status Code:405 Method Not Allowed
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Authorization:Bearer YLPwKSpBsBrFUemRHdjz....
Connection:keep-alive
Content-Length:417
Content-Type:application/json
Host:localhost:670
Origin:http://localhost:660
Referer:http://localhost:660/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
Request Payloadview source
{id:1, ContactOrganisation:Cow on a Mission, ContactPerson:Ben Drury, Phone:07980567574,…}
Address: "----"
ContactOrganisation: "----"
ContactPerson: "Ben"
CreatedBy: "----"
CreatedOn: "2014-03-03T16:40:50.037"
Description: "----"
Email: "---"
Organistion: "----"
Phone: "----"
Website: "http://www.cogiva.com"
id: 1
Response Headersview source
Access-Control-Allow-Headers:Authorization, Content-Type
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Allow:GET,POST,OPTIONS
Cache-Control:no-cache
Content-Length:72
Content-Type:application/json; charset=utf-8
Date:Thu, 17 Apr 2014 13:56:38 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
ConsoleSearchEmulationRendering
如果我通过REST控制台执行此操作并将ID添加到其工作的URL。但直接来自这段代码:
this.model.set($(e.currentTarget).data("modelfield"), $(e.currentTarget).val());
console.log("model",this.model);
this.model.save({
success: function(model, response){
console.log("Yup!");
$(self.el).find('.update_loader').removeClass("running");
$(self.el).find('.update_loader').addClass("done");
},
error: function(){
console.log("No!");
$(self.el).find('.update_loader').removeClass("running");
$(self.el).find('.update_loader').addClass("error");
}
});
在帖子之前的控制台上的模型,肯定有一个ID。为什么不能正确形成URL?
模型定义:
define([
'jquery',
'underscore',
'backbone',
], function ($, _, Backbone){
var ContactModel = Backbone.Model.extend({
url: "http://localhost:670/api/contacts"
});
return ContactModel;
});
答案 0 :(得分:20)
设置Model.url
只是为模型提供一个常量URL。如果要附加模型ID,则需要指定Model.urlRoot
。这将生成“/ api / contacts / id”形式的URL:
var ContactModel = Backbone.Model.extend({
urlRoot: "http://localhost:670/api/contacts"
});
或者,如果模型存储在集合中,则可以将Collection.url
设置为相同。请参阅Model.url
的说明:
委托Collection#url生成URL,因此如果此类的所有模型共享一个公共根URL,请确保已定义URL或urlRoot属性。 ID为101的模型存储在Backbone.Collection中,其URL为“/ documents / 7 / notes”,将具有以下URL:“/ documents / 7 / notes / 101”
答案 1 :(得分:2)
通过在url
定义上设置明确的ContactModel
属性,可以屏蔽Model.url
的默认行为。请改用Model.urlRoot
:
var ContactModel = Backbone.Model.extend({
urlRoot: "http://localhost:670/api/contacts"
});
答案 2 :(得分:2)
您覆盖url
属性。这就是为什么它是固定的 - 骨干调用这个属性/函数来获取url。默认的url
函数实现使用urlRoot
。
将代码更改为此应该有效:
var ContactModel = Backbone.Model.extend({
urlRoot: "http://localhost:670/api/contacts"
});
请检查:model.url
如果您的网址格式特殊(不是urlRoot/id
),则需要使用函数覆盖url
以提供自定义实现。