首页 > 代码库 > nodejs 动态创建二维码

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="http://www.mamicode.com/newQrCode?url=‘+ data.data +‘"/>‘);              
          }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 =http://www.mamicode.com/ req.body;
    var result = JSON.stringify({code:0, data:‘http://www.baidu.com/&id=‘ + data.data});
    //console.log(result)
    res.end(result);

});

 

nodejs 动态创建二维码