cycle.js如何启用访问控制允许来源(CORS)

时间:2019-01-18 03:43:09

标签: javascript reactive cyclejs

使用cyclejs javascript反应框架进行实验,我是这个范例的新手。

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

  

跨源请求被阻止:同源策略禁止阅读   位于的远程资源   https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=。   (原因:CORS标头“ Access-Control-Allow-Origin”缺失)

我了解为什么会返回错误。服务器节点。在这种情况下,未设置标头

  

access-control-allow-origin:*

  

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

将要求应用CORS约束。

我无法找到节点服务器在何处,如何或是否添加CORS标头。节点Server.js代码可以做到,但是我需要帮助来触发添加CORS标头。

谢谢。

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都没有区别。

我的标题是:

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

2 个答案:

答案 0 :(得分:1)

感谢您指出正确的方向,感谢Wikipedia阻止了该请求。在进一步研究中,该解决方案非常简单。在请求字符串中添加“&origin = *”,Wikipedia将返回CORS策略。就是这样。

答案 1 :(得分:0)

维基百科不提供所需的标头,因此请求被阻止。您无法更改。但是您可以使用https://corsproxy.github.io

这样的跨源代理