我的快递应用程序(用于文件服务)每当请求方法" OPTIONS"由我的客户端应用程序发送给它。这是代码:
import express from 'express'
import * as middleware from './middleware'
let App = express();
// Logger
App.use((req, res, next) => {
console.log(req.method, req.url)
next()
})
// Cors
App.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
next();
});
// Middleware
// App.use('/', middleware.authenticate)
// Root
App.get('/', (req, res) => {
res.send('Test!')
})
App.listen(3030, () => {
console.log('It\'s working!')
})
在我的另一个客户端(也是跨域)我得到" 300"随机:
当" 300"时,似乎没有真正的模式。正如您所见,会发生错误。
以下是我的客户发送的请求的代码:
import axios from 'axios'
import Store from '../store'
let api_url = 'http://127.0.0.1:8000/api'
let media_url = 'http://127.0.0.1:3030'
let urls = {
api: api_url,
media: media_url,
}
const Request = {
GET: function(url, server) {
this.getAuthtoken();
return axios.get(urls[server || 'api'] + url, this.config)
},
getAuthtoken: function() {
let token = Store.getState().auth.key;
if (token !== null) {
this.config.headers['Authorization'] = "Token " + token
} else {
delete this.config.headers.Authorization;
}
},
config: {
headers: {}
}
}
// The request
Request.GET('/', 'media').then((response) => {
console.log(response)
})
答案 0 :(得分:0)
Chrome似乎是错误的。我的扩展都没有导致问题,但清除所有数据和重置chrome修复了问题。