Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Post request developed by Wechat

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you the content of a sample analysis of Post requests developed by Wechat. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1.post request

Wx.request (OBJECT)

Wx.request

The HTTPS request is initiated. One WeChat Mini Programs can only have 5 network requests to connect at the same time.

Describe on the official website

The parameter name type is required, indicating that urlString is the developer server interface address dataObject, String No requested parameter headerObject No set header for the request, ReferermethodString cannot be set in header, default is GET. Valid values: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECTsuccessFunction whether the callback function returned by the developer service is successfully received Res = {data: 'content returned by developer server'} failFunction No API calls failed callback function completeFunction No API calls the finished callback function (both successful and failed calls will be executed)

WeChat Mini Programs example

Wx.request ({url: 'test.php', / / is only an example, not the real interface address data: {x:', y:'}, header: {'content-type':' application/json'}, success: function (res) {console.log (res.data)}})

This request GET method is ok, and the header header may not be added.

But POST has a bigger problem.

I use the following code for debugging (Code 1):

Wx.request ({url: ApiHost +'/? service=default.getOrderInfo', data: {'order_id': order_id}, method:' POST', success: function (res) {/ / console.log (res)) If (res.data.ret) {/ / something to do} else {/ / something to do}} fail: function (res) {console.log (res);}})

Pay attention to the following figure and the tips in the Wechat development tool:

2016-12-21_111056.png

The POST request will put the value of data in Request Payload instead of Query String Parameters. If the backend server does not pay attention, it will not be able to get the data.

There are a lot of changes on the Internet, like this. -add header head

Wx.request ({url: ApiHost +'/? service=default.getOrderInfo', data: {/ / data urlencode encoding, post 'order_id='+order_id}, method:' POST', header: {'content-type':'application/x-www-form-urlencoded'}, success: function (res) {/ / console.log (res)) If (res.data.ret) {/ / something to do} else {/ / something to do}} fail: function (res) {console.log (res);}})

If it is modified in this way, the backend does not have to deal with it specially.

But.

Because you still want to do it in a standard way, you have to modify the back-end server.

What I use here is the Phalapi framework. I recommend it.

If (DI ()-> request- > getHeader ('content-type')) {$contentType = DI ()-> request- > getHeader (' content-type');} if (! empty ($contentType) & & (strtolower (@ $contentType) = 'application/json')) {$HTTP_RAW_POST_DATA = isset ($GLOBALS [' HTTP_RAW_POST_DATA'])? $GLOBALS ['HTTP_RAW_POST_DATA']: "{}" DI ()-> request = new PhalApi_Request (array_merge ($_ GET,json_decode ($HTTP_RAW_POST_DATA, true));}

Finally, the code is debugged and passed on pc. Standard requests are used, and application/x-www-form-urlencoded is not used.

But. Use the real machine to debug, why can not receive the request parameters. Strange things.

Finally, through the analysis of grasping the bag

Real machine end

POST /? service=default.getOrderInfo HTTP/1.0Host: proxyConnection: closeContent-Length: 43Content-Type: application/jsonAccept-Encoding: gzip, deflateAccept: * / * User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 9 / 3 / 5 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13G36 MicroMessenger/6.5.1 NetType/WIFI Language/zh_CNReferer: https://servicewechat.com/###/0/page-frame.htmlAccept-Language: zh-cn {"order_id": "011T00wO0gZVR72P89tO0DFNvO0T00w0"}

Pc simulation developer

POST /? service=default.getOrderInfo HTTP/1.0Host: proxyConnection: closeContent-Length: 43Origin: http://###.appservice.open.weixin.qq.comX-Requested-With: XMLHttpRequestUser-Agent: Mozilla/5.0 (Windows NT 6.3 WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 appservice webview/100000content-type: application/jsonAccept: * / * Referer: https://servicewechat.com/####/devtools/page-frame.htmlAccept-Encoding: gzip, deflate, br {"order_id": "011T00wO0gZVR72P89tO0DFNvO0T00w0"}

Finally, the difference is found:

Content-Type and content-type

The simulator defaults to content-type

The real machine defaults to Content-Type.

Add processing Content-Type to the back-end server and it's done.

Thank you for reading! This is the end of the article on "sample Analysis of Post requests for Wechat Development". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report