Comparison and analysis of the difference between $ http. post and jQuery. post in AngularJS, angularjshttp. post
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:
Copy codeThe Code is as follows:
$ Http. post ('do-submit. php', myData)
. Success (function (){
// Some code
});
JQuery:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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.