Ajax validation errors

0

Hello good day I have a question about how to make an error or an exception in Ajax.

My problem is this:

When I select a city that does not have zones, (records in BD) I require an error message to appear.

However, currently the program brings the select empty

How can I make an exception to be thrown if the function returns data, so the Ajax comparison of if(!datos) does not work.

function obtenerZonas(ciudad) {
    $.ajax({
       url : "obtenerZonas.jsp?v_ciu=" + ciudad, /* se envia por GET */
       cache: false,
       dataType : 'html',
       success : function(datos) {
            if(!datos){
             $("#erroro").html("La ciudad Seleccionada No tiene Zonas. Error"+ ciudad );    
            }else{
            $('#zona').html(datos);
            }
       },
       error: function( ) {
             $("#errorz").html("Se presento un problema, Ciudad No. = " + ciudad);             
         } 
    });

}

The solution should be something like this:

My problem is that the error jumps in the new comparison because the value is null ...

But when the problem occurs in a microzone ... there is no way to validate the error, what should I do?

I hope you made me understand .. !!

Thanks !!

    
asked by Andres Felipe Diaz 11.04.2017 в 17:51
source

1 answer

1

From what I understand in success of ajax , variable datos trea text HTML where% is select . Something like this:

datos = '<select...><option..></option>...</select>'

But when the ciudad does not have zonas , it returns the select empty . Something like this:

datos = '<select...></select>'

In this case, the variable datos is not empty , but we can see that it does not have tags option .

One solution may be to check that the variable datos does not have the text <option , that is, only a select empty.

Example:

if(datos.indexOf('<option') === -1) {
  $("#erroro").html("La ciudad Seleccionada No tiene Zonas. Error"+ ciudad );
}
$('#zona').html(datos);

// Update

In the image you can see that the container of the error message is called errorz and if you want that the select does not appear, then you can do this:

if(datos.indexOf('<option') === -1) {
  $("#errorz").html("La ciudad Seleccionada No tiene Zonas. Error"+ ciudad );
  $('#zona').html('');
} else {
  $('#errorz').html('');
  $('#zona').html(datos);
}
    
answered by 11.04.2017 / 18:09
source