首页 > 解决方案 > cycle.js 如何启用 Access-Control-Allow-Origin (CORS)

问题描述

尝试使用 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”)

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

访问控制允许来源:*

内容类型:应用程序/json;字符集=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

标签: javascriptreactivecyclejs

解决方案


感谢您为维基百科阻止请求指出正确的方向。在进一步研究中,解决方案非常简单。将“&origin=*”添加到请求字符串中,维基百科会返回一个 CORS 策略。而已。


推荐阅读