|
react,使用fetch访问本地json , npm start,运行数据读取正常,代码如下:
getJSON(){
var that =this;
fetch('./product.json')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log(json)
console.log(this)
that.setState({
product:json
})
}).catch(function(ex) {
console.log('parsing failed', ex)
})
}product.json,存放的文件目录,public文件夹中与index.html同目录
图片:product.jpg
通过,上面的步骤,访问正常 编译后 执行npm run build, 生成了build文件夹,然后把文件夹中放到php的服务器中执行,运行http://localhost:81/build/index.html, 发现执行页面中的请求json时,始终报错404 , 找不到json文件查阅很多资料,最后,才发现没有问题,把代码放到根目录访问,使用http://localhost:81/index.html 访问,json便能正常访问了。 解决方案:将代码放到根目录运行 待解决的:如果就是要放到二级目录上,运行,如何解决这个路径问题???有知道方案的欢迎分享 |
|
