Angular.js Post Data mode

Source: Internet
Author: User

The company's project front-end section now instead of angular, everything from scratch, today to record a question about the data request, because get the request method is relatively simple, and post is similar, so we talk about the Post method alone.

The post data on the document is written in several ways, using the $http module, which is commonly written as follows:

 //  simple GET Request Example:   $http ({method:  ' GET '  '/someurl ' ). Then ( function   Successcallback (response) { //  this Callback'll be called asynchronously  //  When the response is available }, function   Errorcallback (response) { //  called Asynchronously if an error occurs  //  or Server returns response with an error status. }); 

Then we will change the mode to post, plus data to try.

$http ({      method:' Post ',      URL:' test.php ',    data:{name:' Test ', age:20},  }). Then (function  successcallback (response) {    alert (' Add success ');     function Errorcallback (response) {    alert (' Add failed ');});

In the PHP file we will write a line Print_r ($_post).

Open Google Browser F12, view the debugging information, found that the data transfer past

But careful friends will find a problem, that is, our transmission is the request Playload, rather than our usual form data. The big difference between the two is that the request playload the way we get through $_post in PHP files. You can see that the printed information returned is empty.

  

Let's change it, plus headers and transformrequest.

$http ({      method:' Post ',      URL:' test.php ',    data:{name:' Test ', age:20},      headers:{' content-type ': ' application/x-www-form-urlencoded '},         function (data) {      return  $.param (data);    }}). Then (function  successcallback (response) {    alert (' Add success ');     function Errorcallback (response) {    alert (' Add failed ');});

Then look at the return value

Succeeded, and at this time the transmission mode became

ok,over!

Angular.js Post Data mode

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.