This article talks about the Post request in the development of WeChat applet. If you don’t know about the Post request in the development of WeChat applet or are interested in the Post request in the development of WeChat applet, then Let’s take a look at this article together. Okay, without further ado, let’s get to the point!
##wx.request(OBJECT)wx.request
Initiated is an HTTPS request.
A WeChat applet can only have 5 network request connections at the same time.
Official website description
Type | Required | Description | |
---|---|---|---|
String | ## is | Developer server interface address | |
##Object, String | No | Requested parameters | header |
Object | No | Set the request header, Referer cannot be set in the header | method |
String | No | Default is GET, valid values: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT | ##success |
Function | No | Received the successful return from the developer serviceCallback function, res = {data: 'Content returned by the developer server' } | fail |
Function | No | Callback function for interface call failure | complete |
Function | No | The callback function at the end of the interface call (will be executed if the call is successful or failed) | WeChat Mini Program Example ## wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '' , y: '' }, header: { 'content-type': 'application/json' }, success: function(res) { console.log(res.data) } }) Copy after login
But POST has a big problem. 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 == 200) { //something to do } else{ //something to do } } fail: function (res) { console.log(res); } }); Copy after login POST request will put the value of data in the Request Payload instead of the Query String Parameters. If the backend server does not pay attention, it will not be able to get the data. wx.request({ url: ApiHost + '/?service=default.getOrderInfo', data: { //数据urlencode方式编码,变量间用&连接,再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 == 200) { //something to do } else{ //something to do } } fail: function (res) { console.log(res); } }); Copy after login But... I am using the Phalapi framework here, 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))); } Copy after login code one on the PC. Use standard requests and do not use the application/x-www-form-urlencoded mode. But...when I use a real machine for debugging, why can't I receive therequest parameters again? Strange things. . . . . . . . . Finally through packet capture analysis
POST /?service=default.getOrderInfo HTTP/1.0 Host: proxy Connection: close Content-Length: 43 Origin: http://###.appservice.open.weixin.qq.com X-Requested-With: XMLHttpRequest User-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/100000 content-type: application/json Accept: */* Referer: https://servicewechat.com/####/devtools/page-frame.html Accept-Encoding: gzip, deflate, br {"order_id":"011T00wO0gZVR72P89tO0DFNvO0T00w0"} Copy after login Finally found the difference: Content-Type and content-type
|
The above is the detailed content of Detailed explanation of Post request in WeChat applet development. For more information, please follow other related articles on the PHP Chinese website!