Changing $ .getJson to $ .ajax

0

This code works perfectly:

$(function(){
    $('#id_categoria').change(function(){
        if( $(this).val() ) {
            $('#bairros').hide();
            $('.carregando').show();
            $.getJSON('sub_categorias_post.php?search=',{id_categoria: $(this).val(), ajax: 'true'}, 
                function(j){
                    var options = '<option value="">Escolha o Bairro</option>'; 
                    for (var i = 0; i < j.length; i++) {
                        options += '<option value="' + j[i].id + '">' + j[i].nome_sub_categoria + '</option>';
                    } 
                    $('#bairros').html(options).show();
                    $('.carregando').hide();
                });
        } else {
            $('#bairros').html('<option value="">– Escolha o Bairro –</option>');
        }
    });
});

But I wanted to access a file inside another page and by $ .getJson does not work. I'm trying to do it more ta giving error. It does not load my combo;

$.ajax({
    type: "POST",
    url: "webservice/sub_categorias_post.php",
    //contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: { "id_categoria":  $(this).val()}
    success: function (j) {
        var options = '<option value="">Escolha Subcategoria</option>'; 
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].id + '">' + j[i].nome_sub_categoria + '</option>';
            } 
            $('#bairros').html(options).show();
            $('.carregando').hide();
        }
    });
        }else{
            $('#bairros').html('<option value="">– Escolha Subcategoria –</option>');

        }
    });
});
    
asked by anonymous 03.11.2018 / 15:03

0 answers