nodejs + express + Ajax的get方式提交表单,可以吗

nodejs + express + Ajax的get方式提交表单,可以吗


胡说叔叔
浏览 816回答 3
3回答

温温酱

componentDidMount: function() {//ajax请求var xmlhttp;if(window.XMLHttpRequest) {xmlhttp = new XMLHttpRequest();}else if(window.ActiveXObject) {xmlhttp = new ActiveXObject(‘Microsoft.XMLHTTP’);}else {alert(‘必须提高浏览器版本才能浏览!’);return false;}//回调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);}以上是前端的代码。后台其实逻辑如下: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));});}

芜湖不芜

其实就是使用express做后台接口,可以看一下express的文档。或者网上有很多express的代码。123456var&nbsp;express&nbsp;=&nbsp;require('express');var&nbsp;app&nbsp;=&nbsp;express();app.get('/',&nbsp;function&nbsp;(req,&nbsp;res)&nbsp;{&nbsp;&nbsp;res.send('Welcome');})app.listen(3000);&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Node.js