使用 Polymer iron-ajax 和 Node.js 发出 CORS 请求
Make CORS Request with Polymer iron-ajax and Node.js
我正在尝试使用 Polymer 和 Node 检索数据,但很难获得有效响应。我收到飞行前响应错误,提示 access-control-allow-origin
不被允许。
我在 localhost:4001
上是 运行 Polymer,在 localhost:8080
上是 Node。
如何配置节点或客户端加载响应?
客户
<iron-ajax id="ajaxUser"
url="http://localhost:8080/node/api/mssql/login"
method="post"
handle-as="json"
Content-Type="application/json"
headers='{"Access-Control-Allow-Origin": "*"}'
params="[[params]]"
on-response="saveUserCredentials"
last-response="{{user}}"></iron-ajax>
节点
const corsOptions = {
allowedHeaders: ['Content-Type', 'Access-Control-Allow-Origin']
}
app.options('*', cors(corsOptions))
...
app.use((req, res, next) => { // Enable Cross-Origin Resource Sharing (CORS)
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, x-api-key")
next()
})
错误
Failed to load
http://localhost:8080/login?username=user&password=password:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:4001' is therefore not allowed access.
The response had HTTP status code 400.
问题代码段中的节点配置无法处理 OPTIONS
请求。
为确保 CORS preflights 得到正确处理,考虑安装 npm cors
包:
npm install cors
然后做这样的事情:
var express = require('express')
, cors = require('cors')
, app = express();
app.options('*', cors()); // preflight OPTIONS; put before other routes
这将处理预检响应和其他 CORS 方面,而无需您在应用程序代码中从头开始手动编写自己的处理。
https://www.npmjs.com/package/cors#configuration-option 包含所有选项的更多详细信息。
我正在尝试使用 Polymer 和 Node 检索数据,但很难获得有效响应。我收到飞行前响应错误,提示 access-control-allow-origin
不被允许。
我在 localhost:4001
上是 运行 Polymer,在 localhost:8080
上是 Node。
如何配置节点或客户端加载响应?
客户
<iron-ajax id="ajaxUser"
url="http://localhost:8080/node/api/mssql/login"
method="post"
handle-as="json"
Content-Type="application/json"
headers='{"Access-Control-Allow-Origin": "*"}'
params="[[params]]"
on-response="saveUserCredentials"
last-response="{{user}}"></iron-ajax>
节点
const corsOptions = {
allowedHeaders: ['Content-Type', 'Access-Control-Allow-Origin']
}
app.options('*', cors(corsOptions))
...
app.use((req, res, next) => { // Enable Cross-Origin Resource Sharing (CORS)
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, x-api-key")
next()
})
错误
Failed to load
http://localhost:8080/login?username=user&password=password:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:4001' is therefore not allowed access. The response had HTTP status code 400.
问题代码段中的节点配置无法处理 OPTIONS
请求。
为确保 CORS preflights 得到正确处理,考虑安装 npm cors
包:
npm install cors
然后做这样的事情:
var express = require('express')
, cors = require('cors')
, app = express();
app.options('*', cors()); // preflight OPTIONS; put before other routes
这将处理预检响应和其他 CORS 方面,而无需您在应用程序代码中从头开始手动编写自己的处理。
https://www.npmjs.com/package/cors#configuration-option 包含所有选项的更多详细信息。