3 回答

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個贊
componentDidMount: function() {
//ajax請求
var xmlhttp;
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}else if(window.ActiveXObject) {
xmlhttp = new ActiveXObject(‘Microsoft.XMLHTTP’);
}else {
alert(‘必須提高瀏覽器版本才能瀏覽!’);
return false;
}
//回調(diào)
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4) {
if(xmlhttp.status == 304 || (xmlhttp.status >= 200 && xmlhttp.status < 300)) {
var renderMessage = JSON.parse(xmlhttp.responseText);
if(this.isMounted()){
this.setState({
data: renderMessage,
});
}
}
}
}.bind(this);
//請求
xmlhttp.open(‘post’,’/photo.list.server’,true);
xmlhttp.setRequestHeader(“Content-Type”,“application/x-www-form-urlencoded;charset=utf-8”);
xmlhttp.send(null);
}
以上是前端的代碼。
后臺其實(shí)邏輯如下:
var mongoose = require(‘mongoose’);
var PhotoList = mongoose.model(‘PhotoList’);
module.exports = function(req, res, next) {
PhotoList.find({}, function(err, docs) {
if(err) {
res.end(‘Error’);
return next();
}
res.send(JSON.stringify(docs));
});
}

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個贊
其實(shí)就是使用express做后臺接口,可以看一下express的文檔。或者網(wǎng)上有很多express的代碼。
1 2 3 4 5 6 | var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Welcome'); }) app.listen(3000); |
- 3 回答
- 0 關(guān)注
- 492 瀏覽
添加回答
舉報