Javascript TypeError:无法读取属性';邮政';“无效”的定义;
我刚开始使用React,当我尝试进行Ajax调用时,遇到了一些麻烦 我得到: 内联JSX脚本:29Uncaught TypeError:无法读取null的属性“post” 我不知道为什么我会出错 我已经测试过,如果我将:Javascript TypeError:无法读取属性';邮政';“无效”的定义;,javascript,jquery,ajax,api,reactjs,Javascript,Jquery,Ajax,Api,Reactjs,我刚开始使用React,当我尝试进行Ajax调用时,遇到了一些麻烦 我得到: 内联JSX脚本:29Uncaught TypeError:无法读取null的属性“post” 我不知道为什么我会出错 我已经测试过,如果我将:{this.state.post}从渲染函数中移除,则警报(数据)正确响应 谁能帮帮我吗 <script type="text/jsx"> var SomeComponent = React.createClass({
{this.state.post}
从渲染函数中移除,则警报(数据)
正确响应
谁能帮帮我吗
<script type="text/jsx">
var SomeComponent = React.createClass({
render: function(){
return(
<h3>{this.props.header}</h3>
);
}
});
React.render( <SomeComponent header="Dette er en tittel :)"/>, document.getElementById('content'));
var EinarsComponent = React.createClass({
componentDidMount: function(){
$.ajax({
url: "https://api.bring.com/shippingguide/api/postalCode.html",
data: {clientUrl: "localhost", pnr: "4879"},
success: function(data){
this.setState({post: data});
alert(data);
}.bind(this)
});
},
render: function(){
return(
<div>{this.state.post}</div>
);
}
});
React.render(<EinarsComponent />, document.getElementById('hei'));
</script>
var SomeComponent=React.createClass({
render:function(){
返回(
{this.props.header}
);
}
});
React.render(,document.getElementById('content'));
var EinarsComponent=React.createClass({
componentDidMount:function(){
$.ajax({
url:“https://api.bring.com/shippingguide/api/postalCode.html",
数据:{clientUrl:“localhost”,pnr:“4879”},
成功:功能(数据){
this.setState({post:data});
警报(数据);
}.绑定(此)
});
},
render:function(){
报税表(
{this.state.post}
);
}
});
React.render(,document.getElementById('hei'));
组件的初始状态从未设置,默认为null
。在组件中添加函数getInitialState
:
getInitialState: function(){
return {
post: null // Or some other default value that you fancy
};
}