Размещать полезную нагрузку и получать ответ с AngularJs и PHP - PullRequest
0 голосов
/ 25 июня 2018

Вот как я пытаюсь опубликовать 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, и это все, что я придумал.

1 Ответ

0 голосов
/ 25 июня 2018
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");
        });
    }]);

Для получения дополнительной информации перейдите по этой ссылке

Надеюсь, это поможет вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...