search
HomeWeChat AppletWeChat DevelopmentPost request for WeChat development pitfalls

1.post request

wx.request(OBJECT)
wx.request
initiates an HTTPS request. A WeChat applet can only have 5 network request connections at the same time.
Official website description

Parameter name Type Required Description
url String is developed by Or server interface address
data 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 Receive the callback function returned successfully by the developer service, res = {data: 'Return from the developer server Content'}
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 applet 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 for WeChat development pitfalls

2016-12-21_111056.png

POST request will put the value of data in the Request Payload instead of the Query String Parameters. If the backend server If you don't pay attention, you won't 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 real machine debugging, why can't I receive the request parameters? 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 find the difference :
Content-Type and content-type
The default of the simulator is content-type
The default of the real machine is Content-Type
The back-end server adds processing of Content-Type and it is done.

【Related recommendations】

1. WeChat public account platform source code download

2. 小 Pigcms (PigCms) micro-e-commerce System operation version (independent Weidian mall + three-level distribution system)

3. WeChat People Network v3.4.5 Advanced Commercial Edition WeChat Rubik’s Cube Source Code

The above is the detailed content of Post request for WeChat development pitfalls. 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
使用http.PostForm函数发送带有表单数据的POST请求使用http.PostForm函数发送带有表单数据的POST请求Jul 25, 2023 pm 10:51 PM

使用http.PostForm函数发送带有表单数据的POST请求在Go语言的http包中,可以使用http.PostForm函数发送带有表单数据的POST请求。http.PostForm函数的原型如下:funcPostForm(urlstring,dataurl.Values)(resp*http.Response,errerror)其中,u

Python 3.x 中如何使用urllib.request.urlopen()函数发送POST请求Python 3.x 中如何使用urllib.request.urlopen()函数发送POST请求Jul 31, 2023 pm 07:10 PM

Python3.x中如何使用urllib.request.urlopen()函数发送POST请求在网络编程中,常常需要通过HTTP协议发送POST请求来与服务器进行交互。Python提供了urllib.request.urlopen()函数来发送各种HTTP请求,其中包括POST请求。本文将详细介绍如何使用urllib.request.urlop

如何使用golang中的http.Post函数发送POST请求并获取响应如何使用golang中的http.Post函数发送POST请求并获取响应Nov 18, 2023 am 08:05 AM

如何使用golang中的http.Post函数发送POST请求并获取响应在使用golang进行网络编程时,http包是我们经常使用的一个重要模块。其中,http.Post函数是一个非常实用的函数,可以方便地发送POST请求并获取响应结果。下面将介绍如何使用http.Post函数发送POST请求并获取响应的具体步骤和代码示例。步骤一:导入http包在代码中首先

如何在FastAPI中处理POST请求并返回JSON响应如何在FastAPI中处理POST请求并返回JSON响应Jul 29, 2023 pm 03:08 PM

如何在FastAPI中处理POST请求并返回JSON响应FastAPI是一个快速(高性能)、易用、并且基于标准Python类型提示的现代Web框架。它具有强大的异步支持,可以轻松处理高并发情况。在FastAPI中,我们可以使用简洁的代码来处理POST请求,并返回JSON响应。本文将介绍如何在FastAPI中完成这个任务,并提供相应的代码示例。首先,我们需要创

PHP中POST请求的正确用法PHP中POST请求的正确用法Mar 27, 2024 pm 03:15 PM

PHP中POST请求的使用是在网站开发中常见的操作,通过POST请求可以向服务器发送数据,例如表单数据、用户信息等。正确使用POST请求可以确保数据安全性和准确性,下面将介绍PHP中POST请求的正确用法,并提供具体的代码示例。1.PHP中POST请求的基本原理在PHP中,通过使用$_POST全局变量可以获取通过POST方法提交的数据。POST方法将表单数

PHP微信开发:如何实现消息加密解密PHP微信开发:如何实现消息加密解密May 13, 2023 am 11:40 AM

PHP是一种开源的脚本语言,广泛应用于Web开发和服务器端编程,尤其在微信开发中得到了广泛的应用。如今,越来越多的企业和开发者开始使用PHP进行微信开发,因为它成为了一款真正的易学易用的开发语言。在微信开发中,消息的加密和解密是一个非常重要的问题,因为它们涉及到数据的安全性。对于没有加密和解密方式的消息,黑客可以轻松获取到其中的数据,对用户造成威胁

学习Go语言文档中的net/http.Post函数发送POST请求学习Go语言文档中的net/http.Post函数发送POST请求Nov 04, 2023 am 11:39 AM

学习Go语言中的网络编程是非常重要的一部分,其中发送POST请求是不可或缺的一环。本文将介绍如何使用Go语言文档中的net/http.Post函数来发送POST请求,包括具体的代码示例。首先,我们需要了解POST请求是什么,是一种发送数据到服务器的请求方式。与GET请求不同,POST请求可以发送更多的数据,并且不会将数据暴露在URL中。通常情况下,我们使用P

PHP入门指南:POST请求和响应PHP入门指南:POST请求和响应May 20, 2023 pm 05:52 PM

在Web开发中,交互式应用程序允许用户与网站互动。HTTP协议被设计为可以在服务器和客户端之间传输数据。PHP是一种Web开发语言,可用于处理HTTP请求和响应。本文将介绍如何使用PHP处理POST请求和响应。首先,我们将简要介绍HTTP协议的工作原理,然后讨论如何使用PHP的内置函数处理POST请求和响应。最后,我们将讨论一些最佳实践,以确保您的代码安全和

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use