骨干获取请求是OPTIONS方法 [英] Backbone Fetch Request is OPTIONS method

查看:168
本文介绍了骨干获取请求是OPTIONS方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下URL的骨干集合对象的http://本地主机:8080 / API /菜单/ 1 /独具特色的。我试图执行读取操作来检索URL收集和解析它。然而,在服务器端,我看到这个请求的方法类型选项。该服务器仅想支持GET方法。我不知道骨干是怎么搞清楚什么方法类型的使用,以及为什么它改变OPTIONS方法类型随机有时。我使用的是Node.js的服务器处理的请求。这$ C $下面c是pretty很多我做了什么。

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.

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();

请帮忙,谢谢!

推荐答案

这是一段时间,但我记得以前碰到这个即将到来。有两件事情,这可能是:骨干默认试图做的RESTful API调用后端,这意味着GET,POST,PUT和DELETE。

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.

许多后端并没有与真正的REST支持和唯一支持GET和POST建成。当骨干发送PUT或DELETE命令,您的浏览器(不是主干)会自动发送一个OPTIONS请求先来看看它是否允许做这类请求。如果您的服务器不正确地回答这个调用将失败,并可能骨干不会做任何事情。

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.

要解决这个集 Backbone.emulateHTTP = TRUE; 或你的服务器中正确答案选项调用。查看更多信息的文档: http://backbonejs.org/#Sync-emulateHTTP

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

另一个问题是,你正在做的Ajax请求跨域/子域,你需要正确启用CORS。这也包括正确回答OPTIONS请求。

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.

这篇关于骨干获取请求是OPTIONS方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆