1. Question:
The backend cannot receive the data sent by $http.post in AngularJs, and it is always displayed as null
Sample code:
$http.post(/admin/KeyValue/GetListByPage,{ pageindex: 1,pagesize: 8}) .success(function(){ alert("Mrjing"); });The code is not wrong, but the data cannot be received in the background. Why is this?
Using Firefox Monitoring: The parameters are in JSON format
Use Google Monitoring: The method of transferring parameters is request payload
It can be found that the parameter transfer method is request payload, the parameter format is json, and not form parameter transfer, so in the background, the parameters cannot be received by receiving form data.
When submitting a POST form request, the Content-Type used is application/x-www-form-urlencoded, and if the POST request using native AJAX does not refer to it.
For fixed request header, the default Content-Type is text/plain; charset=UTF-8, and the Content-Type here is:
--------------------------------------------------------------------------------------------------------------------------------
2. Solution:
Directly upload the code:
//The parameter to be passed through post var data = {pageindex: 1,pagesize: 8,},//The address of post request url = "/admin/KeyValue/GetListByPage",//Change the parameter passing method to formpostCfg = {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("Mrjing");});Next, let’s look at the monitoring tool:
Firefox Monitoring: Parameter type has become form data
Google Monitoring:
Now the parameter transmission method has become the form method, and the backend can receive parameters normally!
The above is the relevant knowledge about the reason why the data sent by $http.post in AngularJs cannot be received by the backend. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!