我使用(Dropzone js)上传了ajax文件.它将文件发送到我的hapi服务器.我意识到浏览器发送了一个PREFLIGHT OPTIONS METHOD.但我的hapi服务器似乎没有发送正确的响应标头所以我在chrome上遇到错误.这是我得到的错误
XMLHttpRequest cannot load http://localhost:3000/uploadbookimg. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
这是hapi js路由处理程序
server.route({
path: '/uploadbookimg',
method: 'POST',
config: {
cors : true,
payload: {
output: 'stream',
parse: true,
allow: 'multipart/form-data'
},
handler: require('./books/webbookimgupload'),
}
});
Run Code Online (Sandbox Code Playgroud)
根据我的理解,hapi js应该从Pre-fight(OPTIONS)请求发送所有cors头.不明白为什么不是
来自Chrome的网络请求/响应
**General**
Request Method:OPTIONS
Status Code:200 OK
Remote Address:127.0.0.1:3000
**Response Headers**
view parsed
HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
cache-control: …Run Code Online (Sandbox Code Playgroud) 我正在使用与 axios 的反应来获取一些信息。我正在向 auth 用户发送 JWT 令牌,并在控制台浏览器中收到响应
“从源'http://localhost:3000' 访问 XMLHttpRequest at 'https://beer-tonight.herokuapp.com/beer/getBeerStatus' 已被 CORS 策略阻止:Access 不允许请求标头字段授权-预检响应中的 Control-Allow-Headers。”
这是我的代码:
componentWillMount(){
let token = localStorage.getItem('token');
axios.get('https://beer-tonight.herokuapp.com/beer/getBeerStatus', {headers: {Authorization : 'Bearer ' + token}}).then(
result => {
console.log('yey');
});
}
Run Code Online (Sandbox Code Playgroud)
错误:
当我使用 POSTMAN 时,我得到了正确的答案。
邮递员输入:
ps我已经添加了:
app.use((req, res,next)=>{
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorisation');
next();
Run Code Online (Sandbox Code Playgroud) node.js ×2
axios ×1
cors ×1
dropzone.js ×1
hapijs ×1
javascript ×1
react-native ×1
reactjs ×1
rest ×1