$ .each on request with ajax from php driver

0

Hi, I'm starting to work with ajax and php I have a problem I'm making an ajax request, a php controller post and I'm returning the json format from the controller .. the data I need is perfect and I've made the test . in my condition if there is an equal, I have to address an alert otherwise .. my problem is that if it is not the same, it shows me the alert, but when it is true or it shows me the alert and directs me, I am doing something wrong in the $ .each ???

$('#klik').click(function(){
        $.ajax({
            url: '<?php echo base_url();?>home/validar',
            type:'POST',
            dataType: 'json',
            success: function(data){

               $.each(data, function(i,item){
                  if (item.Url === 'ventas/index'){

                       window.location.href = 'http://localhost/sis_empresa/ventas';
                  }
               });
               alert('no tiene permisos');
              
            } 
        });       
      });
    
asked by Alex Fernando Banegas Gonzalez 22.01.2017 в 20:53
source

1 answer

0
  

Am I doing something wrong in the $.each ?

No, your problem is simply because you do not use conditionals to know whether to take x or y action.

let mustRedirect = false;
$.each(data, function(i,item){
  if (item.Url === 'ventas/index'){
    mustRedirect = true;
  }
});

if (mustRedirect) {
  window.location.href = 'http://localhost/sis_empresa/ventas';
} else {
  alert('no tiene permisos');
}
    
answered by 25.01.2017 в 03:23