Changing $.getJson to $.ajax

Asked

Viewed 59 times

0

That 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 the $.getJson does not work. I’m trying to do so more ta giving error. He does not carry 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>');

        }
    });
});
  • $.getJson uses the get method, your ajax first is already using the post type, you need to confirm if it accepts the post

  • Yes I changed the method of receiving the only thing I changed from one to the other is that $.getJson gets a &_REQUEST['id_categoria'] (It works) while $.ajax gets $_POST['id_categoria'].

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.