Ionic Use FAQ (eight)--php unable to get post data for $http

Source: Internet
Author: User
Tags php server

A simple POST request

$http. Post (' do-submit.php ',myData). Success (function() {    //  Some code});

 

However, data submitted with Angularjs $http cannot be obtained through $_request/$_post on the PHP server side.

Instead, you need:

$params= Json_decode (file_get_contents(' Php://input '),true);

What's the solution?

    1. On the server side (PHP) via $params = Json_decode (file_get_contents (' Php://input '), true); Get parameters, small items can be, large items to a change. (Not recommended)
    2. Modify the default handling of 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)
    3. Add headers in $http: {' content-type ': ' application/x-www-form-urlencoded '},
      Transformrequest:function (obj) {
      var str = [];
      For (var p in obj) {
      Str.push (encodeURIComponent (p) + "=" + encodeURIComponent (Obj[p]));
      }
      Return Str.join ("&");
      },
    4. Configure $httpprovider
varMYAPP = Angular.module (' app ',[]); MYAPP. config (function($httpProvider){$httpProvider. Defaults.transformrequest =function(obj) {varstr = []; for(varp in obj) {STR. push (encodeURIComponent (p) + "=" +encodeURIComponent (obj[p]));}returnStr.Join("&");}$httpProvider. Defaults.headers.post ={' Content-type ': ' application/x-www-form-urlencoded '}});

Ionic Use FAQ (eight)--php unable to get post data for $http

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.