这就是我试图将一些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也是陌生的,这就是我想出的一切。
答案 0 :(得分:0)
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
我希望这会对您有所帮助。