html5 - 通過post抓取的頁面數(shù)據(jù) 為啥不能展現(xiàn)在頁面上
問題描述
這是node.js代碼
var http = require('http'), fs = require('fs'), querystring = require('querystring'), url = require('url');http.createServer(function(req,res){ var postdata=''; var query='what'; var pathname = url.parse(req.url).pathname; req.setEncoding('utf8'); if(pathname=='/'){var indexPage = fs.readFileSync('表單.html');res.writeHead(200,{'Content-Type':'text/html'});res.end(indexPage); } if(pathname=='/about'){req.on('data',function(chunk){ postdata += chunk;});req.on('end',function() { console.log(postdata); query = querystring.parse(postdata); console.log(query);});res.writeHead(200, {'Content-Type':'text/plain'});console.log(query.Name);console.log(query.number);res.write(query.number+ 'and '+query.number);res.end(); } else{res.writeHead(404,{'Content-Type':'text/plain'});res.end('Can not find the source'); }}).listen(2000,'127.0.0.1');console.log('The server is running at port 2000');這是html代碼<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>表單填寫</title></head><body><form action='/about' method='post'> <p> Name: <input type='text' name='Name'></p> <p>SchoolNumber:<input type='text' name='number'></p> <p><input type='submit' value='提交'></p></form></body></html>執(zhí)行結(jié)果圖:
求大神解決 小弟感激不盡
問題解答
回答1:原因分析:回調(diào)函數(shù)執(zhí)行順序的問題, 加點(diǎn)打印信息看看
res.write()那一行返回的是你最開始定義的query并沒有執(zhí)行req.on()里面的操作,而這時的query還是你定義的字符串'what',它不是一個json對象,所以就沒有number屬性,所以是undefined。解決方案:你可以把res.write()寫在req.on()里面,像這樣:
怎么沒見你判斷請求方法啊?這個是我之前寫的一個示例,你可以參考一下:
var http=require(’http’);var url=require(’url’);var fs=require(’fs’);var querystring=require(’querystring’);var mgd=require(’./mongodb.js’);http.createServer(function(req,res){ switch(req.method){case ’POST’: update(req,res); break;case ’GET’: get(req,res); break;default:break; }}).listen(8080);function update(req,res){ var pathname=url.parse(req.url).pathname; var postData=’’; /*接收評論*/ if(pathname==’/postComment’){req.addListener(’data’,function(data){ postData+=data;});req.addListener(’end’,function(){ var json=querystring.parse(postData); mgd(function(c){c.insert(’comment’,json,function(){ var json={} json.code=1; res.writeHead(’Content-Type:application/json;charset=UTF-8’); res.write(JSON.stringify(json)); res.end();}); });}) }}function get(req,res){ var pathname=url.parse(req.url).pathname;/*主頁*/ if(pathname===’/’){fs.readFile(’test.html’,function(err,file){ res.end(file);}) } /*獲取評論列表*/ if(pathname==’/comment’){mgd(function(c){ c.find(’comment’,{},function(data){var json={};if(data.length!=0){ json.code=1; json.data=data;}else{ json.code=0; json.data=null;}json=JSON.stringify(json)res.writeHead(’Content-Type:application/json;charset=UTF-8’);res.write(json);res.end(); }) }) }}
mongodb.js的代碼需要的話可以在我的github上看一波,其實(shí)就是一個簡單的評論demo
相關(guān)文章:
1. javascript - jquery選擇的dom元素如何更新?2. 視頻 - html5 video的autoplay 在智能手機(jī)上不運(yùn)作?3. javascript - H5頁面無縫輪播4. python - Django問題 ’WSGIRequest’ object has no attribute ’user’5. mysql輸入賬號密碼后跳出一大堆內(nèi)容后但卻進(jìn)不了mysql?6. javascript - nodejs中使用request庫怎么抓取網(wǎng)頁中的圖片7. mysql服務(wù)無法啟動1067錯誤,誰知道正確的解決方法?8. .......9. 數(shù)據(jù)庫 - mysql boolean型無法插入true10. python - flask jinjia2 中怎么定義嵌套變量
