Home > Blockchain >  How to check if ajax data has some specific value
How to check if ajax data has some specific value

Time:06-13

I need to check my data.accesslocations contains keyword "Test". Are there any built in function to use?

$(document).on('click', '.update_data', function(){ 
               var row_id = $(this).attr("id");  
               
               $.ajax({  
                    url:"./project/userdetail.php",  
                    method:"POST",  
                    data:{row_id:row_id},  
                    dataType:"json", 
                    success:function(data){  
                         
                        if(data.accesslocations.contains("test")){
                                //do something but this doesn't seem to work ...
                           }
         
    
                    
                        
                         $('#employee_id_update').val(data.row_id);  
                         $('#insert').val("Update");
                         $('#new_data_Modal').modal('show'); 
                        
                            
                          
                    }, 
                    error: function(req, status, error) {
                   alert(req.responseText);      
                    }   
               });  
      }); 

data.accesslocations contains some strings such as

ABC
ABC,DEF
ABC,DEF,GHI 

I need to check that data.accesslocations contains those strings and do something.

CodePudding user response:

There at least 2 solutions:

$.ajax({
   ...
    success:function(data) {
        var accesslocations = data.accesslocations;

        // using Regex (case insensitive, only words)

        if (accesslocations.match(/\bTest\b/i)) {
            processResult(accesslocations);
        }
        
        // or using array prototype includes

        var words = accesslocations.split(' ');
        if (words.includes("Test")) {
            processResult(accesslocations);
        }
    }
});
  • Related