【问题标题】:How to grab php data from angularJS?如何从 angularJS 中获取 php 数据?
【发布时间】:2016-04-01 12:44:02
【问题描述】:

我已经能够将 FormData 从 angularJS 发送到 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";

?>

【问题讨论】:

  • 也许这对你有帮助:stackoverflow.com/questions/19383725/…
  • 我看不出这有什么关系?我的问题与 php 后端有关。
  • @TheVillageIdiot 我真的可以在这方面使用你的帮助!

标签: php angularjs


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您可以通过使用从服务器获取响应

    .then(function (data) {}
    

    在“数据”变量中,您可以找到您要查找的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多