首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Restify Delete方法

Restify Delete方法
EN

Stack Overflow用户
提问于 2013-02-04 20:30:27
回答 4查看 6K关注 0票数 1

CORS开始让我头疼了。除了一种方法之外,现在一切都很好。我正在构建一个前端有backbone,后端有node.js/restify的应用程序。server.coffee如下所示:

代码语言:javascript
复制
server.get '/todos', todos.find_all
server.get '/todos/:id', todos.find_by_id
server.del '/todos/:id', todos.delete

但是,每当backbone中的模型调用destroy时,我都会收到这个相当恼人的错误:

MLHttpRequest cannot load http://localhost:8080/todos/. Method DELETE is not allowed by Access-Control-Allow-Methods.

我读过一些关于这方面的文章,并使用restify完成了以下操作:

代码语言:javascript
复制
unknownMethodHandler = (request, response) ->
    if(request.method.toLowerCase() == 'options')
        allowHeaders = ['Accept', 'Accept-Version', 'Content-Type', 'Api-Version']

        if(response.methods.indexOf('OPTIONS') == -1) then response.methods.push('OPTIONS')

        response.header 'Access-Control-Allow-Credentials', true
        response.header 'Access-Control-Allow-Headers', allowHeaders.join(', ')
        response.header 'Access-Control-Allow-Methods', ['GET', 'DELETE', 'TEST!']
        response.header 'Access-Control-Allow-Origin', request.headers.origin

        response.send 204
    else
        response.send new restify.MethodNotAllowedError()

server.on 'MethodNotAllowed', unknownMethodHandler

但即便如此,我还是得到了以下内容作为响应头:

代码语言:javascript
复制
HTTP/1.1 204 No Content
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version
Access-Control-Allow-Methods: GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: X-Api-Version, X-Request-Id, X-Response-Time
Connection: Keep-Alive
Date: Mon, 04 Feb 2013 12:24:25 GMT
Server: restify
X-Request-Id: fbd4e15a-a22e-48b6-bf5c-a46b94926748
X-Response-Time: 0

我就是不明白我做错了什么!

EN

回答 4

Stack Overflow用户

发布于 2013-02-04 22:13:16

如果你期待一个响应,你应该使用'200‘响应代码,而不是204,因为这是一个No Content响应。有关详细信息,请参阅W3C Spec

9.7删除

DELETE方法请求源站删除Request-URI标识的资源。该方法可以被源服务器上的人为干预(或其他手段)所覆盖。即使源站返回的状态码表明操作已经成功,也无法保证客户端已经执行了操作。但是,服务器不应指示成功,除非在给出响应时,它打算删除资源或将其移动到无法访问的位置。

如果响应包括描述状态的实体,则成功响应应为200 (OK);如果操作尚未实施,则成功响应应为202 (接受);如果操作已实施,但响应不包括实体,则成功响应应为204 (无内容)。

如果请求通过缓存,并且Request-URI标识了一个或多个当前缓存的实体,则应将这些条目视为陈旧。对此方法的响应不可缓存。

票数 1
EN

Stack Overflow用户

发布于 2013-07-11 19:45:03

您将在响应头中看到Access-Control-Allow-Origin: *。这来自.../restify/lib/router.js preflight()方法。评论声明“用户需要定义他们自己的.opts处理程序”。

票数 0
EN

Stack Overflow用户

发布于 2017-05-14 03:53:15

使用server.opts方法为OPTIONS请求编写自己的处理程序。下面是你可以使用的例子。

还请告诉我,当从浏览器发出请求时,您是否使用了设置凭据标志为true。在这种情况下,此句柄必须使用访问cookie进行响应。

在下面的示例中,我将返回允许的原点以进行精确匹配。您也可以将其调整为子字符串匹配。但始终返回响应头'Access-Control-Allow- origin‘中请求头Origin中找到的确切值。这是一个很好的实践。

代码语言:javascript
复制
server.opts('/api/(.)*', (req, res) => {
const origin = req.header('origin');
const allowedOrigins = ['example.com', 'example.org'];
if (allowedOrigins.indexOf(origin) === -1) {
    //origin is not allowed
    return res.send(405);
}
//set access control headers to allow the preflight/options request
res.setHeader('Access-Control-Allow-Origin', header);
res.setHeader('Access-Control-Allow-Headers', 'Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version');
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,PATCH,DELETE,OPTIONS');

// Access-Control-Max-Age header catches the preflight request in the browser for the desired
// time. 864000 is ten days in number of seconds. Also during development you may want to keep


   // this number too low e.g. 1.
    res.setHeader('Access-Control-Max-Age', 864000);
    return res.send(200);
  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14686853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档