php - $.getJSON returning null

851

I am using this very common script to populate a with the name of states from a Country.

<script>
    $(function(){
        $('#country_id').change(function(){
            if( $(this).val() ) {
                $('.receiving').show();
                $.getJSON('server_site_state.php',{term: $(this).val(), ajax: 'true'}, function(j) {
                    var options = '<option value="">Inicio</option>';
                    for (var i = 0; i < j.length; i++) {
                        options += '<option value="' + j[i].id + '">' + j[i].value + '</option>';
                    }
                    $('#state_id').html(options).show();
                    $('.receiving').hide();
                });
            } 
            else {
                $('#state_id').html('<option value="">Select...</option>');
            }
        });
    });
</script>

The thing is thatj is alwaysnull after the call, therefore the<option> field is not populated

I know that the call'server_site_state.php',{ term: $(this).val(), ajax: 'true' } is ok, because I see the URL properly formed in the http log

Also, if I call it in the browser:

http://server_side_state.php?term=18&ajax=1

I get, what I believe, is the right return:

[
    {
        "id": "Atlantico Sur",
        "value": "5604"
    },
    {
        "id": "Buenos Aires",
        "value": "5605"
    },
    {
        "id": "Capital Federal",
        "value": "5606"
    },
    {
        "id": "Catamarca",
        "value": "5607"
    },
    {
        "id": "Chaco",
        "value": "5608"
    },
    {
        "id": "Chubut",
        "value": "5609"
    },
    {
        "id": "Cordoba",
        "value": "5610"
    },
    {
        "id": "Corrientes",
        "value": "5611"
    },
    {
        "id": "Entre Rios",
        "value": "5612"
    },
    {
        "id": "Formosa",
        "value": "5613"
    },
    {
        "id": "Jujuy",
        "value": "5614"
    },
    {
        "id": "La Pampa",
        "value": "5615"
    },
    {
        "id": "La Rioja",
        "value": "5616"
    },
    {
        "id": "Mendoza",
        "value": "5617"
    },
    {
        "id": "Misiones",
        "value": "5618"
    },
    {
        "id": "Neuquen",
        "value": "5619"
    },
    {
        "id": "Rio Negro",
        "value": "5620"
    },
    {
        "id": "Salta",
        "value": "5621"
    },
    {
        "id": "San Juan",
        "value": "5622"
    },
    {
        "id": "San Luis",
        "value": "5623"
    },
    {
        "id": "Santa Cruz",
        "value": "5624"
    },
    {
        "id": "Santa Fe",
        "value": "5625"
    },
    {
        "id": "Santiago del Estero",
        "value": "5626"
    },
    {
        "id": "Tierra del Fuego",
        "value": "5627"
    },
    {
        "id": "Tucuman",
        "value": "5628"
    }
]

Those are the States of Argentina FYI

Any help would be very appreciated.

415

Answer

Solution:

You can add method to handle success and error cases, to get more info, like:

$.getJSON( ... ).done( ... ).fail( function(xhr, ajaxOptions, thrownError) { ... } ).always(...);

In the case of fail you can get more info using parameters in the handler like

function(xhr, ajaxOptions, thrownError) 

People are also looking for solutions to the problem: php - in_array() for array of array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.