【问题标题】:Post payload and receive response with AngularJs and PHP使用 AngularJs 和 PHP 发布有效负载并接收响应
【发布时间】:2018-06-25 13:50:34
【问题描述】:

这就是我尝试将一些 JSON 发布到 php 文件的方式

$http({method: 'POST', url: ApiService.Url("checkout.php"), data: "My JSON payload", cache: false});

这是我要发布到的 PHP 文件

<?php
// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);

// header('Content-Type: application/json');
echo "MAMAMAMAMAMAAMAMAMAMAMAMAMMA";

我想要的是将有效负载发布到 PHP,然后得到响应,我该怎么做?这是我第一次使用 Angular,除此之外我也是 vanilla JS 的新手,这就是我想出的全部。

【问题讨论】:

    标签: javascript angularjs json


    【解决方案1】:
    I am creating a function that will post data to the server and inform you with the response.
    
    var ajaxApp = angular.module("ajaxApp", []);
    
        ajaxApp.controller("CompaniesCtrl", ['$scope', '$http', '$q', function($scope, 
           $http, $q) {
    
    
       // This function will take params object as a parameter, will make a post 
      //request to the server and return the promise. 
    
          var postData  = function (params) {
                var deferred = $q.defer();
                var post = $http({
                    method: "POST",
                    url: "YOUR_URL",
                    dataType: 'json',
                    data: params,
                    headers: { "Content-Type": "application/json" }
                });
    
                post.success(function (data, status) {
                    deferred.resolve(data);
                });
    
                post.error(function (data, status) {
                    deferred.reject(null);
                });
                return deferred.promise;
            };
    
            postData().then(function(data) {
                // This function will be executed when api call is success
                console.log("Data posted");
                console.log(data);
            }, function() {
              // This function will be executed when api call fails.
                console.log("Error Occured");
            });
        }]);
    

    欲了解更多信息,请访问link

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-04
      • 1970-01-01
      • 2011-09-11
      相关资源
      最近更新 更多