不允许获取补丁请求

Koc*_*r4d 35 javascript php ruby-on-rails fetch-api

我有两个应用程序,一个是反应前端,第二个是rails-api应用程序.

我一直很高兴使用isomorphic-fetch,直到我需要将PATCH方法发送到服务器.

我正进入(状态:

Fetch API cannot load http://localhost:3000/api/v1/tasks. Method patch is not allowed by Access-Control-Allow-Methods in preflight response.
Run Code Online (Sandbox Code Playgroud)

但来自服务器的OPTIONS响应包括Access-Control-Allow-Methods列表中的PATCH方法:

在此输入图像描述

这是fetch的实现方式:

const API_URL = 'http://localhost:3000/'                                            
const API_PATH = 'api/v1/'

fetch(API_URL + API_PATH + 'tasks', {
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  method: 'patch',                                                              
  body: JSON.stringify( { task: task } )                                        
})
Run Code Online (Sandbox Code Playgroud)

POST,GET,DELETE设置几乎相同,它们工作正常.

知道这里发生了什么吗?

更新:

方法补丁区分大小写:

https://github.com/github/fetch/blob/master/fetch.js#L200

不确定这是故意还是错误.

更新2

这是预期的,方法类型PATCH需要区分大小写.将行从fetch方法更新为:

method: 'PATCH'
Run Code Online (Sandbox Code Playgroud)

解决了这个问题.

https://github.com/github/fetch/issues/254

dro*_*att 6

我使用Rack :: Cors与reactJS前端和rails API有一个非常相似的问题,并且添加patch到允许的方法列表为我解决了问题.

config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: [:get, :post, :patch, :options]
  end
end
Run Code Online (Sandbox Code Playgroud)