Below I will share with you a brief discussion of the WeChat JSSDK issue with ajax requests for different pages. It has a good reference value and I hope it will be helpful to everyone.
Problem scenario:
I have a form content on page A that needs to be printed using a Bluetooth printer, fill in the form information, and request it through ajax The information is sent to the B file (PHP) for processing. After jumping to the B page through the form normally, it can be printed out, because in addition to the PHP that processes the data, the B page also contains JS code, which is the JSSDK part. However, when sent through an ajax request, there is no response, and all jssdk except wx.ready and wx.config cannot be executed. (Both files A.B have wx.config and wx.ready configurations)
For example, the parameters for sending data:
wx.invoke('sendDataToWXDevice',data,function(res){ //微信提供的接口,只有这么一个回调函数,如果通过ajax请求,则无法进入回调函数 if(res.err_msg=='sendDataToWXDevice:ok') { alert("打印成功"); window.history.go(-1); } else { alert("打印失败"); } });
So I guessed that by requesting the PHP file, the returned data contained the JS code part of my B page. In other words, page A had wx.config and wx.ready, and page B returned The content also has these two interfaces. In order to eliminate this problem,
I removed all WeChat interfaces on page B. Only the content that needs to be sent after processing the form is saved in an input.
//返回 处理后的数据 echo ''; echo ''; ?>
The original jump page printing was changed to asynchronous request printing. In fact, sending data from WeChat to the device is still executed on the A page, and the B file is only responsible Returns the data passed from A to B and processed through encryption. Therefore, delete all mixed html and JS codes in the B file. What should be noted is whether the config configuration contains the beta:true attribute, because it involves the hardware SDK. , and whether the hardware SDK interface to be used is also filled in the interface list parameters
. I felt that my idea was correct at the time. But every time I use document.write to write down the returned results in the callback functions of wx.invoke including openWXDeviceLib, sendDataToWXDevice, etc., but I soon find that the output after wx.invoke is not executed every time, and my original page The content was also cleared. I was not aware of this problem at this time, so I kept testing until I output the data returned by page B before invoke (the above document.write content is my success callback function after the request is successful) Output), I realized that the data I wanted to send had become undefind when I executed the invoke method. I suddenly discovered that the original content of my page had been cleared (because it had previously jumped to B The page was printed, so it was blank, ~ ~, I got used to it, and didn’t notice any changes). Later I switched to alert output, and I could print normally. That is to say, in the ajax encapsulated callback function of JQUERY, use document.write to output , will reset the page content (the reason why I use document.write to output is that in a certain stage of the previous test, the config originally used as debugging popped up information through the alert window, but I don’t know why it used document.write The form is written on the page. I have forgotten what happened at that time, which caused this error, but I am still aware of this problem of JQ)
Remember:Using document.write to output content in JQuery's ajax success callback function will reset the page content. As for other callback functions, you can take a look at it yourself.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Solve the loader problem of vue-cli created projects
Use vue to write a sample code for a carousel chart that imitates a simple book
The above is the detailed content of What are some problems with WeChat JSSDK in ajax requesting different pages?. For more information, please follow other related articles on the PHP Chinese website!