1. Question:
The backend receives no data sent by $http.post in Angularjs and always appears null
Sample code:
$http. Post (/admin/keyvalue/getlistbypage,
{
pageindex:1,
pagesize:8
})
. Success (function () {
alert ("Mr Jing");
});
The code is not wrong, but in the background but not receive data, this is why?
Monitor with Firefox: parameter is JSON format
With Google monitoring: the way to pass the reference is request payload
You can find that the reference mode is request payload, the parameter format is JSON, not the form parameter, so receive the parameter in the background to receive the form data is not received
When a post form request is submitted, the Content-type used is application/x-www-form-urlencoded, whereas a POST request using native Ajax does not refer
Set request Header Requestheader, the default content-type is Text/plain;charset=utf-8, and the content-type here is:
--------------------------------------------------------------------------------
2. Solution:
Directly on the code:
parameter var data to be passed by post
= {
pageindex:1,
pagesize:8,
},
//post requested address
URL = "/admin/keyvalue/ Getlistbypage ",
//Change the way parameters are passed to form
postcfg = {
headers: {' content-type ': ' application/ X-www-form-urlencoded '},
transformrequest:function (data) {return
$.param (data);
}
;
Send a POST request to get the data
$http. Post (URL, data, postcfg)
. Success (function (response) {
alert ("Mr Jing");
Next look at the monitoring tool:
Firefox monitor: parameter types have become form data
Google surveillance:
Now the mode of reference becomes form, and then the backend can receive the parameters normally!
The above is a small series to introduce the back-end to receive Angularjs in the $http.post sent data analysis and solutions related knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!