如何从angularJS获取php数据?

时间:2015-12-27 12:35:48

标签: php angularjs

我已经能够将angularData中的FormData发送到php,但我不知道该怎么做。我目前正在尝试将$ base64变量放入我的angularJS中,但我对如何去做这件事感到非常难过。官方angularJS中的文档对我没什么帮助

https://docs.angularjs.org/api/ng/service/ $ HTTP

JS

$scope.MakeGray_Button = function(){
    if ($scope.imageUrl) {
        var MakeGray_Form = new FormData();
        MakeGray_Form.append("FileName", $scope.imageUrl);
        $http({
        method : "POST",
        url    : "../opencv/MakeGray/MakeGray.php",
        data   : MakeGray_Form,
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
        }).
        success(function(){                     
           // some magic code here that grabs the $base64 variable from php
        })
        .error(function(){});
    }
    else{
        alert("Please upload an image");
    }
}

PHP

<?php

$imgname = $_POST["FileName"];
$inputDir = "../../uploads/" . $imgname;
$outputDir = "../../processed/" . $imgname;

$MakeGray = "./makegray " . $inputDir . " " . $outputDir;
$runExec  = exec($MakeGray, $out);

$type = pathinfo($outputDir, PATHINFO_EXTENSION);

$data = file_get_contents($outputDir);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

echo "$base64";

?>

1 个答案:

答案 0 :(得分:0)

您可以在成功回调中添加参数以从服务器获取响应(如果可用)。

var base64 = ''; 
$http({
    method : "POST",
    url    : "../opencv/MakeGray/MakeGray.php",
    data   : MakeGray_Form,
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined}
    }).
    success(function(response){                     
          base64 = response; //response will contain whatever server echos to it's standard output
    })
    .error(function(){});