Comparison and Analysis of $ http. post and jQuery. post in AngularJS _ AngularJS

Source: Internet
Author: User
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.

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.