javascript Backbone Fetch Request 是 OPTIONS 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11300874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:41:19  来源:igfitidea点击:

Backbone Fetch Request is OPTIONS method

javascriptjquerybackbone.jsfetchoptions

提问by Jeffrey Chen

I have a Backbone Collection object with the following URL "http://localhost:8080/api/menu/1/featured". I am trying to perform a fetch operation to retrieve the collection from the url and parse it. However, on the server side, the method type that I see for this request is OPTIONS. The server is only suppose to support GET method. I am not sure how Backbone is figuring out what method type to use, and why it changes to OPTIONS method type randomly sometimes. I am using a Node.js server to process the request. This code below is pretty much what I did.

我有一个带有以下 URL 的 Backbone Collection 对象“http://localhost:8080/api/menu/1/featured”。我正在尝试执行提取操作以从 url 检索集合并解析它。但是,在服务器端,我看到的此请求的方法类型是 OPTIONS。服务器仅假设支持 GET 方法。我不确定 Backbone 如何确定要使用的方法类型,以及为什么它有时会随机更改为 OPTIONS 方法类型。我正在使用 Node.js 服务器来处理请求。下面的代码几乎就是我所做的。

var FeaturedCollection = Backbone.Collection.extend({
    model:FeaturedContent,
    url:function () { return url_featured; },
    parse:function (response) {
        console.log(response);
        return response;
    }
});

var featuredCollection = new FeaturedCollection();
featuredCollection.fetch();

Please help, thanks!

请帮忙,谢谢!

回答by Mauvis Ledford

It's been awhile, but I remember coming across this before. There's two things this could be: Backbone by default tried to do RESTful API calls to your backend, this means GET, POST, PUT, and DELETE.

已经有一段时间了,但我记得以前遇到过这个。这可能是两件事: 默认情况下,Backbone 尝试对后端进行 RESTful API 调用,这意味着 GET、POST、PUT 和 DELETE。

Many backends weren't built with real REST support and only support GET and POST. When Backbone sends a PUT or DELETE command your browser (not Backbone) automatically sends an OPTIONS request first to see if it's allowed to make these kinds of requests. If your server answers improperly this call will fail and probably Backbone won't do anything.

许多后端没有构建真正的 REST 支持,只支持 GET 和 POST。当 Backbone 发送 PUT 或 DELETE 命令时,您的浏览器(不是 Backbone)会自动首先发送 OPTIONS 请求,以查看是否允许发出此类请求。如果您的服务器响应不当,此调用将失败,并且 Backbone 可能不会做任何事情。

To get around this set Backbone.emulateHTTP = true;Or have your server properly answer OPTIONS calls. See the documentation for more info: http://backbonejs.org/#Sync-emulateHTTP

解决此问题Backbone.emulateHTTP = true;或者让您的服务器正确应答 OPTIONS 呼叫。有关更多信息,请参阅文档:http: //backbonejs.org/#Sync-emulateHTTP

The other issue is that you're making ajax requests cross-domain / sub-domain and you need to properly enable CORS. This also includes properly answering OPTIONS requests.

另一个问题是您正在跨域/子域发出 ajax 请求,您需要正确启用 CORS。这还包括正确回答 OPTIONS 请求。

回答by soupasouniq

I had the exact same problem as OP - using Backbone and NodeJS to save data via a CORS POST request would constantly send an OPTIONS http request header, and not trigger the POST request at all.

我遇到了与 OP 完全相同的问题 - 使用 Backbone 和 NodeJS 通过 CORS POST 请求保存数据会不断发送 OPTIONS http 请求标头,而根本不会触发 POST 请求。

Apparently CORS with requests that will "cause side-effects on user data" will make your browser "preflight" the request with the OPTIONS request header to check for approval, before actually sending your intended HTTP request method. https://developer.mozilla.org/en-US/docs/HTTP_access_control#Overview

显然,带有“对用户数据产生副作用”的请求的 CORS 将使您的浏览器“预检”带有 OPTIONS 请求标头的请求以检查批准,然后再实际发送您想要的 HTTP 请求方法。 https://developer.mozilla.org/en-US/docs/HTTP_access_control#Overview

This thread was what solved my problem - How to allow CORS?

这个线程解决了我的问题 -如何允许 CORS?

The poster used some middleware to approve PUT/GET/POST/DELETE requests like so -

海报使用了一些中间件来批准 PUT/GET/POST/DELETE 请求,如下所示 -

res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
...
next();

and the next(); would allow the OPTIONS check to continue on to the POST request.

和下一个();将允许 OPTIONS 检查继续到 POST 请求。

Worked like a dream for me, hope it helps someone else too.

对我来说就像做梦一样,希望它也能帮助其他人。

回答by eabait

Backbone.js maps CRUD methods to HTTP. Taken from Backbone's source code:

Backbone.js 将 CRUD 方法映射到 HTTP。摘自 Backbone 的源代码:

var methodMap = {
  'create': 'POST',
  'update': 'PUT',
  'delete': 'DELETE',
  'read':   'GET'
};
Backbone.sync = function(method, model, options) {
   var type = methodMap[method];

Probably the problem resides on your node.js backend.

问题可能出在您的 node.js 后端。

回答by user1004988

What version of backbone are you using? I had exactly the same issue, but then realised I had been using an old version of backbone (0.3.3) in a tutorial. Upgraded the link to the latest backbone.js (0.9.2) and underscore.js(1.3.3) and it sends as a GET.

你用的是什么版本的主干?我遇到了完全相同的问题,但后来意识到我一直在教程中使用旧版本的主干 (0.3.3)。将链接升级到最新的backbone.js (0.9.2) 和underscore.js(1.3.3) 并作为GET 发送。