dongxing1412 2015-12-27 12:35
浏览 74

如何从angularJS获取php数据?

I've been able to sent FormData from angularJS to php, but I don't know how to do the reverse. I'm currently trying to get the $base64 variable into my angularJS, but I'm quite stumped on how to go about doing so. The documentation in the official angularJS doesn't help me much either

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条回答 默认 最新

  • douzhan1935 2016-07-18 06:04
    关注

    You can add a parameter in success callback to fetch the response from server if available.

    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(){});
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?