cycle.js如何启用Access Control Allow Origin(CORS)



对cyclejs javascript反应式框架进行实验,我对这个范式很陌生。

我设置了一个从外部URL(en.wikipedia.org)读取的流,并得到以下结果:

阻止跨来源请求:同源策略不允许读取位于的远程资源https://en.wikipedia.org/w/api.php?action=query&list=搜索&format=json&srsearch=。(原因:CORS标头"Access Control Allow Origin"丢失)

我理解返回错误的原因。服务器,节点。,在这种情况下,没有设置标题

访问控制允许来源:*

内容类型:application/json;charset=utf-8

将需要应用CORS约束。

我无法找到节点服务器在哪里、如何或是否添加CORS标头。节点Server.js代码可以完成这项工作,但我需要帮助来触发CORS头的添加

谢谢。

包.json是:

{
"name": "wikipedia",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "parcel -p 1235 index.html",
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "cb",
"license": "ISC",
"dependencies": {
"@cycle/dom": "^22.3.0",
"@cycle/http": "^15.1.0",
"@cycle/run": "^5.2.0",
"npm": "^6.6.0",
"xstream": "^11.10.0"
},
"devDependencies": {}
}

节点包cors是否安装没有区别。

我的标题是:

=== Response headers (1.424 KB)  ===
accept-ranges: bytes
age: 0
backend-timing: D=149145 t=1547782329117450
cache-control: private, must-revalidate, max-age=0
content-disposition: inline; filename=api-result.json
content-encoding: gzip
content-length: 1409
content-type: application/json; charset=utf-8
date: Fri, 18 Jan 2019 03:32:09 GMT
p3p: CP="This is not a P3P policy! …lAutoLogin/P3P for more info."
server: mw1224.eqiad.wmnet
server-timing: cache;desc="pass"
set-cookie: WMF-Last-Access=18-Jan-2019;Pa…Tue, 19 Feb 2019 00:00:00 GMT
set-cookie: WMF-Last-Access-Global=18-Jan-…Tue, 19 Feb 2019 00:00:00 GMT
strict-transport-security: max-age=106384710; includeSubDomains; preload
vary: Accept-Encoding,Treat-as-Untru…,Cookie,Authorization,X-Seven
via: 1.1 varnish (Varnish/5.1), 1.1…1), 1.1 varnish (Varnish/5.1)
x-analytics: ns=-1;special=Badtitle;https=1;nocookies=1
x-cache: cp1079 pass, cp2012 pass, cp5007 pass, cp5008 pass
x-cache-status: pass
x-content-type-options: nosniff
X-Firefox-Spdy: h2
x-frame-options: SAMEORIGIN
x-powered-by: HHVM/3.18.6-dev
x-search-id: cfqpbykh6k905h7hol9taip2o
x-varnish: 494419235, 536993066, 474976751, 593813616

=== Request headers (362 B)  ===
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Host: en.wikipedia.org
Origin: http://localhost:1235
Referer: http://localhost:1235/
TE: Trailers
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linu…) Gecko/20100101 Firefox/64.0

感谢您为我指明了维基百科阻止请求的正确方向。经过进一步研究,解决方案非常简单。在请求字符串中添加"&origin=*",维基百科就会返回CORS策略。就是这样。

Wikipedia不提供所需的标头,因此请求被阻止。你无法改变这一点。但是您可以使用这样的跨原点代理https://corsproxy.github.io

相关内容

最新更新