var Movie = React.createClass({
getInitialState: function() {
$.ajax({
url: "getjsonarray.php",
dataType: 'json',
method: "POST",
data: {
startpoint: 0,
perpage: 2
},
success: function(data) {
this.setState({
json: data
}, function() {
}.bind(this));
}.bind(this),
});
return null;
},
render: function() {
return (
<div>
{
this.state.json.map(function(object, i){
return (
<div key={i}>
<h1>Movie {i}</h1>
<h2>Genre {i}</h2>
</div>
);
})
}
</div>
);
}
});
ReactDOM.render(<Movie/>, document.getElementById('container'));
var Movie = React.createClass({
getInitialState: function() {
$.ajax({
url: "getjsonarray.php",
dataType: 'json',
method: "POST",
data: {
startpoint: 0,
perpage: 2
},
success: function(data) {
this.setState({
json: data
}, function() {
}.bind(this));
}.bind(this),
});
return null;
},
render: function() {
return (
<div>
{
this.state.json.map(function(object, i){
return (
<div key={i}>
<h1>Movie {i}</h1>
<h2>Genre {i}</h2>
</div>
);
})
}
</div>
);
}
});
ReactDOM.render(<Movie/>, document.getElementById('container'));
在这里,我试图从后端迭代通过数组通过反应js json数组。但我只得到这个错误,
TypeError: this.state is null
有没有办法用ajax responcejson数组返回值?
这是我从后端获得,
[{"id":"1","image":"http:\/\/images.prd.mris.com\/image\/V2\/1\/Yu59d899Ocpyr_RnF0-8qNJX1oYibjwp9TiLy-bZvU9vRJ2iC1zSQgFwW-fTCs6tVkKrj99s7FFm5Ygwl88xIA.jpg","price":"$1,975,000 ","address":"609 W Gravers Ln","area":"4,820 SqFt","beds":"5","baths":"5","desc":"Situated between fairmount park and the prestigious philadelphia cricket club, this beautiful 2+ acre property is truly","subdesc":"Courtesy of HS Fox & Roach-Chestnut Hill Evergreen"},{"id":"2","image":"http:\/\/images.prd.mris.com\/image\/V2\/1\/vGoNjc2jHGb87GlnnDQlf6LxeOUgIOn0bL6Wvn1nEnig2Ntq6W7xN5cOQBZZeNxl9O42DOkHUw0LNnj1ZB2KHA.jpg","price":"$1,500,000","address":"1220-32 N Howard St","area":"4,900 SqFt","beds":"1","baths":"1","desc":"A once in a lifetime opportunity to own a unique live \/ work space in one of philadelphia's most popular neighborhoods.","subdesc":"Courtesy of ll Banker Preferred-Philadelphia"}]
非常感谢它为我工作:) – vimuth