本文实例为大家分享了nodejs动态创建二维码的具体代码,供大家参考,具体内容如下
<!--弹出二维码--> <div class="qrcode"> <div> <p id="saoma">扫描二维码</p> </div> </div>
js代码:
$.ajax({ type: 'POST', url: '/house', data: { data:'你的数据' }, dataType: 'json', success: function (data) { if (data.code == 0) { alert("成功"); $('#saoma').before('<img class="qrcode_show" src="https://www.gxlcms.com/newQrCode?url='+ data.data +'"alt=""/>'); }else { alert('失败'); } } })
nodejs:
var express = require('express'); var request = require('request'); var url = require('url'); var qrImg = require('qr-image'); //生成二维码 app.get('/newQrCode', function (req, res) { var par = url.parse(req.url, true).query; var thisUrl = par.url; var thisParam = par.id; var imgUrl = thisUrl + '?id=' + thisParam; var img = qrImg.image(imgUrl, { size: 10 }); res.writeHead(200, { 'Content-Type': 'image/png' }); img.pipe(res); });
var bodyParser = require('body-parser'); var urlencodeRarser = bodyParser.urlencoded({ extended: false }); app.post('/house',urlencodeRarser,function (req, res) { var data = req.body; var result = JSON.stringify({code:0, data:'http://www.baidu.com/&id=' + data.data}); //console.log(result) res.end(result); });