不允许获取补丁请求 [英] fetch patch request is not allowed
问题描述
我有两个应用程序,一个是反应前端,第二个是rails-api应用程序。
我一直很高兴使用
这是获取实现的方式:
const API_URL ='http:// localhost:3000 /'
const API_PATH ='api / v1 / '
fetch(API_URL + API_PATH +'任务',{
标题:{
'接受':'应用程序/ json',
'Content-Type' :'application / json'
},
方法:'patch',
body:JSON.stringify({task:task})
})
POST,GET,DELETE的设置几乎相同,并且工作正常。
知道这里发生了什么吗?
更新:
方法补丁区分大小写:
https://github.com/github/fetch/blob/master/fetch.js#L200
不确定这是故意还是错误。
更新2
这意图和方法类型PATCH需要区分大小写。
将该行从fetch方法更新为:
方法:'PATCH'
解决问题。
https://github.com/github/fetch/issues/254
我使用Rack :: Cors与reactJS前端和rails API有一个非常相似的问题,并将 patch
添加到允许的列表中方法为我解决了这个问题。
config.middleware.insert_before 0,Rack :: Cors do
allow do
originins'*'
resource'*',header :: any,methods:[:get,:post,:patch,:options]
end
end
I have two apps one is a react front end and the second one is the rails-api app.
I have been happily using isomorphic-fetch till I needed to send PATCH method to the server.
I am getting:
Fetch API cannot load http://localhost:3000/api/v1/tasks. Method patch is not allowed by Access-Control-Allow-Methods in preflight response.
but the OPTIONS response from the server includes a PATCH method in a list of Access-Control-Allow-Methods:
This is how the fetch is implemented:
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 } )
})
POST, GET, DELETE are set up pretty much the same and they are working fine.
Any idea what is going on here?
UPDATE:
Method patch is case sensitive:
https://github.com/github/fetch/blob/master/fetch.js#L200
Not to sure if this is intended or a bug.
UPDATE 2
This is intended and the method type PATCH needs to be case sensitive. Updating the line from fetch method to:
method: 'PATCH'
fixes the problem.
https://github.com/github/fetch/issues/254
I had a very similar problem with reactJS front end and rails API using Rack::Cors, and adding patch
to the list of allowed methods solved the problem for me.
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :patch, :options]
end
end
这篇关于不允许获取补丁请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!