如何将angularjs变量传递给Php页面并将其翻转

时间:2017-11-29 07:26:16

标签: php angularjs ajax http

我正在尝试使用http get将angularjs变量传递给php页面,但无法在php页面中访问它。我想传递一个变量,然后在php页面中对该变量执行一些计算,然后在html页面中再次检索它。  我的代码如下:

## HTML
$scope.postFunc = function(){
            $http.post(
                "calculate.php",
                {sal:$scope.sal}
            ).then(function(data){
                alert($scope.sal);
                $http.get("./calculate.php").then(successCall,errorCall);
            });
        }

function successCall(response){

            $scope.total = response.data;
            alert($scope.total);
        }
        function errorCall(response){
            alert("error");
        }

PHP文件

<?php
$data = json_decode(file_get_contents("php://input"));
    $salary = $data->sal;
    $da = (20*$salary)/100;
    $ta = (30*$salary)/100;
    $hra = (60*$salary)/100;

    $total_salary = ($salary + $da + $ta + $hra);



    echo json_encode($total_salary);

&GT;

我收到错误消息

Trying to get property of non-object in C:\wamp64\www\php\Grade Task\calculate.php on line 3.

1 个答案:

答案 0 :(得分:0)

$http.post("calculate.php", {sal:$scope.sal}).then(function(answerFromServer){
  console.log(answerFromServer);
});

您的服务器(PHP服务器?)需要进行计算并发送带有答案的相应HTTP响应。