【发布时间】:2020-10-08 02:49:51
【问题描述】:
我是 vueJS 的新手,使用的版本是 2.0。我需要从 url 获取一些 JSON 数据。使用 javacsript fetch 方法从 URL 获取数据。下面是我的代码,
function getValue() {
const requestOptions = {
method: "GET",
header: {"Access-Control-Allow-Origin": "GET"}
};
fetch("http://api.plos.org/search?q=title:DNA", requestOptions)
.then(
(response) => {
console.log(response.json());
}
);
}
getValue();
我遇到了类似的 CORS 问题,
Access to fetch at 'http://api.plos.org/search?q=title:DNA' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
我尝试了堆栈溢出中可用的所有可能性。但没有运气。 任何帮助将非常感激。 JSFiddle 链接 - https://jsfiddle.net/m45hsekf/
【问题讨论】:
-
CORS 应该在服务器级别启用,例如节点或 java 应用程序。
标签: javascript vuejs2