search
HomeBackend DevelopmentPHP TutorialDetailed explanation of Post request in WeChat applet development
Detailed explanation of Post request in WeChat applet developmentMar 14, 2018 pm 04:52 PM
postAppletsProgram development

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!

1.post request


##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


##Parameter nameTypeRequiredDescriptionurlStringdataheadermethod
## is Developer server interface address
##Object, String No Requested parameters
Object No Set the request header, Referer cannot be set in the header
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) 
  }
})


This request GET method is OK, and the header does not need to be added.

But POST has a big 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 == 200) {
       //something to do
      }
      else{
       //something to do
      }
    }
    fail: function (res) {
      console.log(res);
    }
  });

Pay attention to the picture below, the prompts in the WeChat development tool:


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.
There are many reform laws on the Internet, which are like this. ----Add the header

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);
    }
  });

If you modify it like this, the backend does not need special processing.

But...

Because I still want to use the standard method, the only way is to modify the backend server.

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)));
}

Finally, I passed the debugging using

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 the

request parameters again? Strange things. . . . . . . . . Finally through packet capture analysis

##real machine end

POST /?service=default.getOrderInfo HTTP/1.0
Host: proxy
Connection: close
Content-Length: 43
Content-Type: application/json
Accept-Encoding: gzip, deflate
Accept: */*
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_CN
Referer: https://servicewechat.com/###/0/page-frame.html
Accept-Language: zh-cn

{"order_id":"011T00wO0gZVR72P89tO0DFNvO0T00w0"}

pc simulation development end


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"}

Finally found the difference:

Content-Type and content-type
The emulator default is content- type
The default for real machines is Content-Type
The back-end server adds the processing of Content-Type and it is done.

The above is all the content of this article. If you don’t know much about it, you can easily master both sides by yourself!



Related recommendations: Detailed explanation of WeChat applet implementation of pull-down loading and pull-up refresh

WeChat applet implements finger zoom picture code sharing

PHP implements WeChat applet payment code sharing

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
小程序能用react吗小程序能用react吗Dec 29, 2022 am 11:06 AM

小程序能用react,其使用方法:1、基于“react-reconciler”实现一个渲染器,生成一个DSL;2、创建一个小程序组件,去解析和渲染DSL;3、安装npm,并执行开发者工具中的构建npm;4、在自己的页面中引入包,再利用api即可完成开发。

浅析php中POST方法带参数跳转页面浅析php中POST方法带参数跳转页面Mar 23, 2023 am 09:15 AM

对于PHP开发者来说,使用POST带参数跳转页面是一项基本技能。POST是HTTP中一种发送数据的方法,它可以通过HTTP请求向服务器提交数据,跳转页面则是在服务器端进行页面的处理和跳转。在实际开发中,我们经常需要使用POST带参数来跳转页面,以达到一定的功能目的。

php怎么判断post有没有提交php怎么判断post有没有提交Mar 21, 2023 pm 07:12 PM

PHP是一种广泛使用的服务器端脚本语言,它可以用于创建交互式和动态的Web应用程序。在开发PHP应用时,我们通常需要通过表单将用户输入数据提交给服务器端处理。然而,有时候我们需要在PHP中判断是否有表单数据被提交,这篇文章将介绍如何进行这样的判断。

python requests post如何使用python requests post如何使用Apr 29, 2023 pm 04:52 PM

python模拟浏览器发送post请求importrequests格式request.postrequest.post(url,data,json,kwargs)#post请求格式request.get(url,params,kwargs)#对比get请求发送post请求传参分为表单(x-www-form-urlencoded)json(application/json)data参数支持字典格式和字符串格式,字典格式用json.dumps()方法把data转换为合法的json格式字符串次方法需要

java如何发起http请求调用post与get接口java如何发起http请求调用post与get接口May 16, 2023 pm 07:53 PM

一、java调用post接口1、使用URLConnection或者HttpURLConnectionjava自带的,无需下载其他jar包URLConnection方式调用,如果接口响应码被服务端修改则无法接收到返回报文,只能当响应码正确时才能接收到返回publicstaticStringsendPost(Stringurl,Stringparam){OutputStreamWriterout=null;BufferedReaderin=null;StringBuilderresult=newSt

NGINX反向代理对HTML页面的POST请求返回405怎么解决NGINX反向代理对HTML页面的POST请求返回405怎么解决May 22, 2023 pm 07:49 PM

实现如下:server{listen80;listen443ssl;server_namenirvana.test-a.gogen;ssl_certificate/etc/nginx/ssl/nirvana.test-a.gogen.crt;ssl_certificate_key/etc/nginx/ssl/nirvana.test-a.gogen.key;proxy_connect_timeout600;proxy_read_timeout600;proxy_send_timeout600;c

PHP代码示例:如何用POST方式传参并实现页面跳转PHP代码示例:如何用POST方式传参并实现页面跳转Mar 07, 2024 pm 01:45 PM

标题:PHP代码示例:使用POST方式传参并实现页面跳转的方法在Web开发中,经常会涉及到如何通过POST方式传递参数,并在服务器端进行处理后实现页面跳转的需求。PHP作为一种流行的服务器端脚本语言,提供了丰富的函数和语法来实现这一目的。下面将通过一个实际的示例来介绍如何使用PHP来实现这一功能。首先,我们需要准备两个页面,一个用来接收POST请求并处理参数

PHP跳转页面并携带POST数据的实现方法PHP跳转页面并携带POST数据的实现方法Mar 22, 2024 am 10:42 AM

PHP是一种广泛应用于网站开发的编程语言,而页面跳转并携带POST数据是在网站开发中常见的需求。本文将介绍如何实现PHP页面跳转并携带POST数据,包括具体的代码示例。在PHP中,页面跳转一般通过header函数实现。如果需要在跳转过程中携带POST数据,可以通过以下步骤完成:首先,创建一个包含表单的页面,用户在该页面填写信息并点击提交按钮。在表单的acti

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment