Many times we need to use ajax to submit post data. Angularjs is similar to jq and also has encapsulated posts.
However, jQuery's post is obviously simpler and more humane than angularjs's post.
AngularJS:
$('',myData)
.success(function(){
// some code
});
jQuery:
$.post('', myData, function() {
// some code
});
It doesn't look like a difference, right? However, the data submitted using angularjs' $http cannot be obtained through $_REQUEST/$_POST on the php server, but requires:
$params = json_decode(file_get_contents('php://input'),true);
Come to get it. What's the reason?
This is because the two posts handle header differently... jQuery will serialize myData as JSON objects, for example:
var myData = { a : 1, b : 2 };
// jQuery will convert myData into a string before posting the data: "a=1&b=2"
And Angular won't.
What is the solution?
1. Introduce jquery, provided that the target user does not mind loading an extra script of dozens of K. (Not recommended)
2. On the server side (PHP), get parameters through $params = json_decode(file_get_contents('php://input'),true); Get the parameters. Small projects are OK, and large projects need to be modified one by one. (Not recommended)
3. Modify the default processing of Angular's $httpProvider:/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/(This is the best way to facilitate future management)
Have you gained a better understanding of the difference between $ and $ in AngularJS? I hope you can gain something after reading this article.