Http://www.cnblogs.com/hsp-blog/p/5919877.html
Today, at the time of the work (Daoteng applet), it was found that sending a POST request to the node backend server could not receive the parameters from the front end. In fact, it is not completely impossible, at the beginning of the period with jquery Ajax in the time there is no problem (the background can accept parameters).
Then go through the Google console to see headers found to be passed when the parameters are different: Figure 1 (JQ in Ajax), Figure 2 (Wx.request () in the applet)
(Figure I) (figure II)
You see? The second-to-last line of each graph form data and Request payload These two different ways of delivery cause the background server to take no parameters. This requires different methods of receiving parameters in the backend server for different ways: Take the node+express+post request as an example (the default parameter is a JSON object)
Way |
Processing |
Form Data |
Req.body |
Request Payload |
var str= ""; Req.on ("Data", function (chunk) {str+=chunk}) |
The Request payload method is to enter the background in a "streaming" way, and it is necessary to listen to the data event to obtain the complete information.
Test code (Form Data):
1 var express = require (' Express '); 2 var router = Express. Router (); 3 4/* GET home page. */5 Router.post ('/', function (req, res, next) {One console.log (req.body) var str= ""; 15 Req.on ("Data", function (dt) { str+=dt17 }) Req.on ("End", function () { console.log (str) ) res.send ({name:1})); module.exports = router;
Results:
Test code (Request Payload):
1 var express = require (' Express '); 2 var router = Express. Router (); 3 4/ * GET home page. * /5 router.post ('/', function (req, res, next) {
var str= ""; 6 Req.on ("Data", function (chunk) {7 str+=chunk 8 }) 9 req.on ("End", function () {Ten Console.log (str) one }) res.send ({name:1})) ; module.exports = router;
Results:
Query string parameters is more common in the console headers appears because .... url?name=1 is the URL address? Parameters such as name=1 appear behind.
Background fetch parameters can be req.query
Summary: is based on different parameters of the way, the background has a different way to obtain, should correspond! The Get method pass arguments in HTTP are all query string parameters
Form data,request payload,query string parameters in the HTTP request and how to receive these parameters in the node server