How to validate value coming from json from php to ajax

0

In php I have the following return:

            $invalido = (object) array(
                'invalido' => true
            );
            echo json_encode($invalido);

I need to display in success of ajax asked if invalido is true . I've tried the following:

success: function(data) {
            //alert(data);

           if(data.invalido == true)
           {
               alert('invalido');
           }

        }

You are not entering if, and the top alert is displaying the following:

{"invalido":true}{"scalar":false}

How do I validate what's coming in invalido ?

I need to access what's coming in this json , access invalido :

{"invalido":true}{"scalar":false}
    
asked by anonymous 09.11.2018 / 18:44

1 answer

0

Try

var data = [{
  "invalido": true
}, {
  "scalar": false
}];

if (data[0].invalido == true) {
  console.log('invalido');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Or

var data = [{
  "invalido": true
}, {
  "scalar": false
}];


$.each(data, function(key, value) {
  if (value.invalido == true)
    console.log("invalido");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
09.11.2018 / 19:21