试图将我的回复转换为 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;却无处可去。
我能够连接到我的服务器并使用获取请求来显示一些文本。然而,当我休息后重新启动服务器时,我遇到了这个错误
\n\nContent 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).\nRun Code Online (Sandbox Code Playgroud)\n\n如果有人能指出我正确的方向,那就太好了。
\n\n如果有帮助的话,我正在使用最新版本的 Firefox。
\n\n编辑:我更改了端口。我现在可以看到我的获取请求,但是,我仍然收到错误
\n\nconst 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})\nRun Code Online (Sandbox Code Playgroud)\n