【发布时间】:2017-10-31 19:29:49
【问题描述】:
我正在为一个使用 JQuery 从远程源加载 JSON 编码数据的 Web 应用程序学习 ReactJS。
在这个示例中,this.state 被手动设置为 JSON 编码的数据
import React, { Component } from 'react';
import Projects from './Components/Projects';
import './App.css';
class App extends Component {
constructor(){
super();
this.state = {
projects: [
{
title: 'Buisness Website',
category: 'Web Design'
},
{
title: 'Social App',
category: 'Mobile Development'
},
{
title: 'Ecommerce Shopping Cart',
category: 'Web Development'
}
]
}
}
render() {
return (
<div className="App">
My App
<Projects projects={this.state.projects}/>
</div>
);
}
}
export default App;
我想使用 Jquery 从远程位置加载数据。我试过了
this.state = $.getJSON('remote.json', function (data) {
console.log(data);
});
不幸的是,在运行该站点时,我收到一个错误:“$”未定义。我怎样才能解决这个问题?请注意,我包括了
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
在 index.html 的头部
编辑:请记住,JSON 文件不会是本地的。
【问题讨论】:
-
你忘记了一些 jquery 版本 :)) 为什么你有 2 个 jquery 文件?
-
你需要从
jquery导入$,像这样:import $ from 'jquery';检查这个答案,它会帮助你:stackoverflow.com/a/44259149/5185595 -
@madalin_ivascu 已编辑 :)
标签: javascript jquery json reactjs