js中調用微信的掃描二維碼功能的實現(xiàn)代碼
關鍵代碼
<html><head> <title> js調用微信掃一掃功能測試 </title> <!--引用微信JS庫--> <script type='text/javascript' src='http://res.wx.qq.com/open/js/jweixin-1.0.0.js'></script> <!--引用jQuery庫--> <script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.min.js'></script></head><body> <input type='button' value='掃一掃' id='scanQRCode'><script type='text/javascript'> //這里【url參數(shù)一定是去參的本網(wǎng)址】 $.get('獲取微信認證參數(shù)的網(wǎng)址?url=當前網(wǎng)頁的網(wǎng)址', function(data){ var jsondata=$.parseJSON(data); wx.config({ // 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印。 debug: false, // 必填,公眾號的唯一標識 appId: jsondata.model.appId, // 必填,生成簽名的時間戳 timestamp: '' + jsondata.model.timestamp, // 必填,生成簽名的隨機串 nonceStr: jsondata.model.nonceStr, // 必填,簽名 signature: jsondata.model.signature, // 必填,需要使用的JS接口列表 jsApiList: [’checkJsApi’, ’scanQRCode’] }); }); wx.error(function (res) { alert('出錯了:' + res.errMsg);//這個地方的好處就是wx.config配置錯誤,會彈出窗口哪里錯誤,然后根據(jù)微信文檔查詢即可。 }); wx.ready(function () { wx.checkJsApi({ jsApiList: [’scanQRCode’], success: function (res) { } }); //點擊按鈕掃描二維碼 document.querySelector(’#scanQRCode’).onclick = function () { wx.scanQRCode({needResult: 1, // 默認為0,掃描結果由微信處理,1則直接返回掃描結果,scanType: ['qrCode'], // 可以指定掃二維碼還是一維碼,默認二者都有success: function (res) { var result = res.resultStr; // 當needResult 為 1 時,掃碼返回的結果 alert('掃描結果:'+result); window.location.href = result;//因為我這邊是掃描后有個鏈接,然后跳轉到該頁面} }); }; });</script></body></html>
注意事項:
“獲取微信認證參數(shù)”這個的前提是您能夠有自己的微信開發(fā)資質,并能獲取到正確的參數(shù)
公眾號的唯一標識 簽名的時間戳 簽名隨機串常見的錯誤
config:invalid signature
解決辦法
“當前網(wǎng)頁的地址”-----哈哈,一定是你寫的不對,這里一定是去參的本網(wǎng)頁的地址最好是在服務器下去測試
總結
到此這篇關于在js中調用微信的掃描二維碼功能的文章就介紹到這了,更多相關js 微信掃描二維碼內容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章: