我试图在我的Node.js应用程序中支持使用Express.js Web框架的CORS.我已经阅读了有关如何处理此问题的Google小组讨论,并阅读了一些有关CORS如何工作的文章.首先,我这样做了(代码是用CoffeeScript语法编写的):
app.options "*", (req, res) ->
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Credentials', true
# try: 'POST, GET, PUT, DELETE, OPTIONS'
res.header 'Access-Control-Allow-Methods', 'GET, OPTIONS'
# try: 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'
res.header 'Access-Control-Allow-Headers', 'Content-Type'
# ...
Run Code Online (Sandbox Code Playgroud)
它似乎不起作用.好像我的浏览器(Chrome)没有发送初始OPTIONS请求.当我刚刚更新资源块时,我需要将跨源GET请求提交到:
app.get "/somethingelse", (req, res) ->
# ...
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Credentials', true
res.header 'Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS'
res.header 'Access-Control-Allow-Headers', 'Content-Type'
# ...
Run Code Online (Sandbox Code Playgroud)
它有效(在Chrome中).这也适用于Safari.
我读过......
在实现CORS的浏览器中,每个跨源GET或POST请求前面都有一个OPTIONS请求,用于检查GET或POST是否正常.
所以我的主要问题是,为什么在我的案例中似乎没有发生这种情况?为什么我的app.options块没有被调用?为什么我需要在主app.get块中设置标题?
我使用React和Express遇到了同构JavaScript应用程序的问题.
我正在尝试使用axios.get在我的组件安装时发出HTTP请求
componentDidMount() {
const url = 'http://ufc-data-api.ufc.com/api/v3/iphone/fighters/title_holders';
axios.get(url).then( res => {
//use res to update current state
})
}
Run Code Online (Sandbox Code Playgroud)
我从API获得状态200 res,但我没有得到任何响应数据并在我的控制台中收到错误
XMLHttpRequest cannot load http://ufc-data-api.ufc.com/api/v3/iphone/fighters/title_holders.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
但是,如果我在server.js中发出请求
const url = 'http://ufc-data-api.ufc.com/api/v3/iphone/fighters/title_holders';
axios.get(url).then(res => {
//console.log(res);
});
Run Code Online (Sandbox Code Playgroud)
它工作正常,我在服务器启动时获得响应数据.这是实际API的问题还是我做错了什么?如果这是一个CORS问题,我猜测server.js中的请求也不起作用?谢谢!
我正在使用 NuxtJS 的$axios模块,并且我正在与一个后端 API 进行交互,该 API 设置了一个身份验证 cookie,以便在登录后进行进一步的身份验证请求。我已经查看了withCredentials: true,但它仍然无法正常协作。
我需要设置特定的标头值还是我的 axios 配置有问题?
我只想保留并使用从成功登录中收到的这个 cookie,并在下一个请求中使用它来发出经过身份验证的请求。
// nuxt.config.js
axios : {
...
credentials: true,
withCredentials : true,
requestInterceptor: (config, {store}) => {
config.headers.common['Content-Type'] = 'application/json';
config.headers.common['API-Key'] = 'my_api_key';
return config
},
...
},
Run Code Online (Sandbox Code Playgroud)
这是我提出 axios 请求的地方
async asyncData({ $axios}) {
try {
// this response sends back an authentication cookie
const login_response = await $axios.$post("my_login_route",{
password : this.password,
username : this.email,
});
if(login_response.success){
// how i send my …Run Code Online (Sandbox Code Playgroud) 我正在阅读本教程以构建节点api:
http://scotch.io/tutorials/javascript/build-a-restful-api-using-node-and-express-4
他们通过getpostman.com了解如何测试发布请求.
但是 - 我不希望我的应用程序响应来自不同域的发布请求.我只希望它响应来自我的域(而不是浏览器)的Rails的发布请求.如何停止接受来自外国来源的这些请求,但允许来自我的rails服务器的请求?
我尝试过从这个链接中找到的中间件,如下所示.但没有奏效.这很容易.有线索吗?
router.all('/', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "https://www.example.com");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "POST GET");
res.header("X-Frame-Options", "ALLOWALL");
res.header("Access-Control-Allow-Credentials", "true");
next();
});
Run Code Online (Sandbox Code Playgroud) javascript ×3
node.js ×3
axios ×2
cors ×2
coffeescript ×1
express ×1
http ×1
module ×1
nuxt.js ×1
reactjs ×1