Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取API无法加载file:///C:/Users/Jack/Desktop/Books_H/book-网站/公共/api/书籍。URL方案必须为";http「;或;https";申请CORS_Javascript_Html_Node.js - Fatal编程技术网

Javascript 获取API无法加载file:///C:/Users/Jack/Desktop/Books_H/book-网站/公共/api/书籍。URL方案必须为";http「;或;https";申请CORS

Javascript 获取API无法加载file:///C:/Users/Jack/Desktop/Books_H/book-网站/公共/api/书籍。URL方案必须为";http「;或;https";申请CORS,javascript,html,node.js,Javascript,Html,Node.js,刚开始在我的学校学习node js。他们给了我们这个完成了一半的任务,我需要让下一个和上一个按钮工作。但是,在运行index.html时,控制台中出现了一些错误。错误是: “获取API无法加载file:///C:/Users/Jack/Desktop/Books_H/book-site/public/api/books.URL方案必须为“http”或“https”才能用于CORS请求。“ 另一个是: “未捕获(承诺中)TypeError:在HTMLDocument.document.addEve

刚开始在我的学校学习node js。他们给了我们这个完成了一半的任务,我需要让下一个和上一个按钮工作。但是,在运行index.html时,控制台中出现了一些错误。错误是:

“获取API无法加载file:///C:/Users/Jack/Desktop/Books_H/book-site/public/api/books.URL方案必须为“http”或“https”才能用于CORS请求。“

另一个是:

“未捕获(承诺中)TypeError:在HTMLDocument.document.addEventListener处获取失败”

我甚至不知道如何开始解决这个问题。有什么帮助吗

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1000, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello</title>
</head>
<body>
    Hello from the other side! <b>Total: <span id="total"></span></b><br/>
    <button id="author">Sort by author</button>
    <button id="title">Sort by title</button>
    <table id="books" border="1">
        <tr>
            <th>
                Author
            </th>
            <th>
                Book Title
            </th>
        </tr>

    </table>
    <script src="index.js"></script>
</body>
</html>

如果这对将来的任何人都有帮助的话,这就是我必须做的。这是所有基本的东西,但我是初学者,所以我们开始吧。打开命令提示符。转到项目的目标(index.js文件所在的位置)并写入:

$ npm init -y
$ npm install -g express-generator
$ npm install express -S
$ npm install connect -S
$ npm install serve-static -S
然后转到server.js文件的目标并写入

$ node server.js

在此之后,我可以在浏览器中运行我的页面,键入URL。

添加到脚本标记
type='text/javascript'
如果您在Windows下,请在本地IIS中创建一个新站点(如果尚未启用Windows组件中的IIS),并将其添加到项目文件夹中。然后打开(或您可以在ISS中为新站点设置的其他端口)

不要从文件浏览器打开文件,而是使用localhost:8080浏览器不允许使用ajax调用的
文件://
URL(出于安全原因)。您需要通过web服务器加载web页面,并使用http://或https://而不是http://通过该web服务器发出ajax请求file://.Do 您需要一个节点服务器来调用计算机中的获取函数吗?我想即使在同一个目录下工作也不行,对吧?这里有一个关于你要问的主题的链接。这可能对你有帮助
node server.js
internal/modules/cjs/loader.js:985 throw err;^错误:找不到模块“C:\mywork\GitHub\myProject\server.js”[90m at Function.module.\u resolveFilename(internal/modules/cjs/loader.js:982:15)[39m[90m at Function.modules/cjs/loader.js:864:27][39m[90m at Function.executeUserEntryPoint as runMain](internal/modules/run\u main.js:74:12)[39m[90m位于internal/main/run_main_module.js:18:47[39m{code:[32m'module_NOT_FOUND'[39m,requireStack:[]
$ npm init -y
$ npm install -g express-generator
$ npm install express -S
$ npm install connect -S
$ npm install serve-static -S
$ node server.js