如何获取本地 json。错误说Unexpected token < ..
。我没有网络错误。
项目列表.js:
import React, { Component } from 'react';
class Itemslist extends Component {
componentDidMount() {
fetch('items.json').then(res => {
console.log(res)
// return res.json()
}).then(res => {
console.log(res)
})
}
}
以下是我在开发工具中得到的回应
Response {type: "basic", url:
"http://localhost:5000/items.json", redirected: false, status: 200, ok: true, …}
body: ReadableStream
bodyUsed: true
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:5000/items.json"
__proto__: Response
虽然响应是 200,但我收到以下错误:
bundle.js:30 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
项目结构:
|---static
| |---reactApp
| |--components
| | |--items.json
| | |--Itemslist.js
| |
| |--App.js