小编Cam*_*Cam的帖子

如何修复 async/await 函数中的“response.json 不是函数”

试图将我的回复转换为 json。我不太确定我收到TypeError: response.json is not a function错误。有人可以指出我正确的方向。提前致谢。

    componentDidMount(){
        this.timingFunction = setInterval(() => this.getAllStations(), 1000);
    }


    async getAllStations(){
        try{
            const response = await(`http:api.bart.gov/api/etd.aspx?cmd=etd&orig=${this.state.selectedStation}&key=${bartKey}&json=y`);
            const data = await response.json();
            console.log(`Here: ${data}`)


        }catch(e){
            console.log(`Error: ${e}`)
        }

    }
Run Code Online (Sandbox Code Playgroud)

我希望看到 json 响应,但收到错误消息。编辑:在 response.json() 前面添加了 await;却无处可去。

javascript reactjs

5
推荐指数
2
解决办法
2万
查看次数

如何修复“内容安全策略:页面的设置阻止加载 http://localhost:8080/favicon.ico(“default-src”)上的资源。”

我能够连接到我的服务器并使用获取请求来显示一些文本。然而,当我休息后重新启动服务器时,我遇到了这个错误

\n\n
Content Security Policy: The page\xe2\x80\x99s settings blocked the loading of a resource at http://localhost:8080/favicon.ico (\xe2\x80\x9cdefault-src\xe2\x80\x9d).\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果有人能指出我正确的方向,那就太好了。

\n\n

如果有帮助的话,我正在使用最新版本的 Firefox。

\n\n

编辑:我更改了端口。我现在可以看到我的获取请求,但是,我仍然收到错误

\n\n
const express = require ('express');\nconst cors = require('cors');\n\nconst app = express();\n\napp.use(cors());\nconst port = 3001;\n// was 8080\n\napp.listen(port, () => {\n    console.log(`Server is up and listening on port ${port}`);\n})\n\napp.get('/', (req, res) => {\n    res.send({\n        express: 'Your express backend is connected to react'\n    })\n})\n
Run Code Online (Sandbox Code Playgroud)\n

node.js express

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

express ×1

javascript ×1

node.js ×1

reactjs ×1