Today I learn how to generate QR codes. I am used to all requests using ajax.
But today I found that jquery's ajax does not support binary, and can only do plain text
So Baidu manually implemented this function
function getBinary(url, args, success) { var xmlhttp = new XMLHttpRequest(); var data = eval(args); var i = 0; for (var key in data) { if (i++ === 0) { url += '?' + key + "=" + data[key]; } else { url += '&' + key + "=" + data[key]; } } ("GET", url, true); = "blob"; = function () { success(); }; (); }
I haven't dealt with XHR problems with different browsers. Just do it in the first line if you need to deal with it.
Requires three parameters
One is the request url, the second is the request parameter, and the third is the processing method after success
Request parameters in this way
{‘param1':1,'param2':2,'param3':3}
The processing method needs to receive a parameter, which is the response data, which is the data in the figure below.
Then the call
function submit() { getBinary("/request/qrCode", {'data': $('#str').val()}, function (data) { var img = $('#qrcode'); (); $('#qrcode').attr('src', (data)); }) }
I'm showing the QR code here
The fifth line of code cannot be missing. If it is missing, it will be fine to execute the method for the first time. If it is executed again, the picture will not change. The page needs to be refreshed.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.