<!--弹出二维码--> <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="/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 img = qrImg.image(thisUrl, { 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); });
之前的代码,二维码扫描成功后,URL后的参数会重复一遍,现在已经修改