This article mainly compares and analyzes the differences between $ http. post and jQuery. post in AngularJS. it is very detailed and is a very good article. we recommend this article to our friends. In many cases, we need to use ajax to submit post data. angularjs is similar to jq and has encapsulated post.
However, jQuery post is much simpler and more user-friendly than angularjs post.
AngularJS:
The code is as follows:
$ Http. post ('Do-submit. php', myData)
. Success (function (){
// Some code
});
JQuery:
The code is as follows:
$. Post ('Do-submit. php', myData, function (){
// Some code
});
Doesn't it look any different? However, the $ http submitted data in angularjs cannot be obtained on the php server through $ _ REQUEST/$ _ POST. Instead, you need:
The code is as follows:
$ Params = json_decode (file_get_contents ('php: // input'), true );
. Why?
This is because the post processing of the header is different between the two ...... JQuery serializes myData as a JSON object, for example:
The code is as follows:
Var myData = {a: 1, B: 2 };
// JQuery converts myData into a string before The post data: "a = 1 & B = 2"
Angular does not.
What is the solution?
1. introduce jquery, provided that the target user does not mind loading dozens of K more scripts. (Not recommended)
2. on the server side (PHP), use $ params = json_decode (file_get_contents ('php: // input'), true). obtain the parameters. for small projects, change the parameters one by one. (Not recommended)
3. modify the default handle for Angular $ httpProvider: http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/ (this is the best way to facilitate future management)
Do you have a further understanding of the difference between $ http. post and jQuery. post in AngularJS? I hope you can get some help after reading this article.