简体   繁体   中英

How can i manipulate an object

This is on my PHP:

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

And this is what I getting for angular:

.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;
    }
}])

Result: Object { msg: "Mesagge for AngularJS" } on the function (res)

So, I need to comparate the object result

I can't use $scope , because at service from angular can't used whit it.

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

your question is not clear , wich object do you want to compare ??

I made this code with jquery to do what I think you are searching for:

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>

I hope that will help you.

You were really close, just had to unpack the response object a bit more.

In your successCallback , use res.data.msg for your evaluation, rather than just res.data .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM