The back-end can not receive the data cause analysis of $http.post sent in Angularjs and its solution _angularjs

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.