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

cycle.js how to enable Access-Control-Allow-Origin (CORS)

正在试验 cyclejs javascript 反应式框架,我是范例的新手。

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

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)

我明白为什么会报错了。服务器 node,在这种情况下,没有设置 header

access-control-allow-origin: *

Content-Type: application/json; charset=utf-8

将需要应用 CORS 约束。

我无法找到节点服务器在何处、如何或是否添加 CORS header。节点 Server.js 代码可以执行此操作,但我需要帮助来触发添加 CORS header.

谢谢。

package.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安装与否没有区别

我的 header 是:

=== 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

维基百科不提供所需的 headers 因此请求被阻止。你无法改变这一点。但是你可以像这样使用跨源代理 https://corsproxy.github.io

感谢您在维基百科阻止请求方面为我指明了正确的方向。进一步研究,解决方案非常简单。将“&origin=*”添加到请求字符串和 Wikipedia returns CORS 策略。就是这样。