亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

html5 - 通過post抓取的頁面數據 為啥不能展現在頁面上

瀏覽:132日期:2023-10-09 17:41:01

問題描述

這是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>執行結果圖:

html5 - 通過post抓取的頁面數據 為啥不能展現在頁面上

html5 - 通過post抓取的頁面數據 為啥不能展現在頁面上

求大神解決 小弟感激不盡

問題解答

回答1:

原因分析:回調函數執行順序的問題, 加點打印信息看看html5 - 通過post抓取的頁面數據 為啥不能展現在頁面上

res.write()那一行返回的是你最開始定義的query并沒有執行req.on()里面的操作,而這時的query還是你定義的字符串'what',它不是一個json對象,所以就沒有number屬性,所以是undefined。解決方案:你可以把res.write()寫在req.on()里面,像這樣:html5 - 通過post抓取的頁面數據 為啥不能展現在頁面上

回答2:

怎么沒見你判斷請求方法啊?這個是我之前寫的一個示例,你可以參考一下:

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上看一波,其實就是一個簡單的評論demo

標簽: Html5
相關文章:
主站蜘蛛池模板: 一级做a爱 一区 | 一级aaa级毛片午夜在线播放 | 91精品视频免费在线观看 | 久久全国免费久久青青小草 | 黄色在线播放网址 | 日本69sex护士泡妞 | 久久黄色网 | 性生活视频网 | 国产视频高清在线 | 国产三级精品三级国产 | www.午夜视频 | 亚洲色图第1页 | 五月婷婷开心中文字幕 | 日本久久草 | 三级毛片大全 | 天天看天天射天天碰 | 欧美精品第二页 | 麻豆影视视频高清在线观看 | 国产露脸150部国语对白 | 欧美日韩中文在线视频 | 国产一区二三区 | 在线免费视频国产 | 免费在线观看黄 | 国产一区二区三区四区小蝌蚪 | 女人被免费看在线看 | 国产 网红 喷水 播放 | 欧美特黄三级成人 | 最新亚洲一区二区三区四区 | 亚洲欧美日韩中文在线制服 | 色综色天天综合网 | 国产一区二区三区在线观看精品 | 啪啪一级视频 | 在线看一区 | 亚洲成人18 | 一级视频在线 | av香港经典三级级 在线 | 日韩免费毛片全部不收费 | 精品视频一区二区三区 | 毛片免费观看的视频在线 | 久草在线视频精品 | 玖玖国产精品视频 |