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