我该如何操纵一个物体

时间:2017-09-26 20:26:42

标签: javascript php jquery angularjs

这是我的PHP:

if(whatever)
{
 echo("GOOD"
}
else
{
    $result['msg'] = "Mesagge for AngularJS";
}
echo json_encode($result);

这就是我对角度的看法:

.service('upload', ["$http", "$q", "$location", function ($http, $q, $location, $scope)
{
    this.uploadFile = function(file, idPunto)
    {
        var deferred = $q.defer();
        var formData = new FormData();
        formData.append("name", idPunto);
        formData.append("file", file);
        return $http.post("Uploads/server.php", formData, {
            headers: {
                "Content-type": undefined
            },
      params: {id: idPunto},
            transformRequest: angular.identity
        })
        .then(function successCallback(res)
        {
            //Here I need to comparate the object result from PHP
            if(res.data == 'message from PHP')
            {
                $location.url("/controlPanel");
            }
            else
            {
              console.log("not is equal");
            }                
            deferred.resolve(res);
        }
        ,function errorCallback(msg, code)
        {
            deferred.reject(msg);
        })
        return deferred.promise;
    }
}])

结果:对象{msg:“AngularJS的Mesagge”} 功能上的(res)

所以,我需要比较对象结果

我无法使用 $ scope ,因为在角色服务时无法使用whit。

//Example of that.
$scope.tableRepeat= dataImages.data;

2 个答案:

答案 0 :(得分:0)

你的问题不明确,你想要比较哪个对象?

我用jquery制作了这个代码来做我认为你正在寻找的东西:

<强> page.php文件:

<?php 
$result['msg'] = "Mesagge for AngularJS";
echo json_encode($result);
?>

<强>的index.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script type="text/javascript" src="jquery-3.2.1.min.js" ></script>
</head>
<body>
<script>
    $(function () {
        $.ajax({
            url:'page.php',
            dataType:'json',
            success:function(data) {
                if(data.msg =="Mesagge for AngularJS")
                {
                    alert(data.msg);
                }else{
                    alert("no data found!");
                }
            }
        });
    });
</script>
</body>
</html>

我希望这会对你有帮助。

答案 1 :(得分:0)

你真的很接近,只需要再解压一下响应对象。

successCallback中,使用res.data.msg进行评估,而不仅仅是res.data