Save variable in JavaScript and pass in the URL of getjSon

0

Well, I'm new to JavaScript, and I need to save the value of the matricula field in a variable and pass it on the URL of a getJson. how can I do this? follows the code below the two functions: Code to save enrollment variable:

$(function($){
    $('#btn_entrar').click(function(){
        var matricula = $('#id_matricula').val();
    });
);

Code to call my getJson:

$("#btn_entrar").click(function(event){
    $(document).ready(function(){
        $.getJSON("URL/'ValorDaVariavelMatricula'", function(data){
        });
    });
 });
    
asked by anonymous 15.09.2015 / 13:10

2 answers

1

It seems to me that you are creating two events for the same #brn_entrar button. Just join the codes. You get the value of the field and put it in the variable matricula , then call the getJSON concatenando this variable to the string url:

$(function(){
    $('#btn_entrar').on("click", function() {
        var matricula = $('#id_matricula').val();

        $.getJSON("URL/" + matricula, function(data){
        });
    });
});
    
15.09.2015 / 13:15
-2

Hello, friend, I think the best solution would be to use ajax , here is the code below:

<script type="text/javascript">
$(document).ready(function() {
    $('#btn_entrar').click(function(){
        var matricula = $('#id_matricula').val();
        $.ajax({
            url: URL,
            type: 'GET',
            dataType: 'json',
            data: {matricula: matricula},
            success: function(json){
                // aqui trata seu json
            },
            error: function(json){
                alert('erro');
            }
        });     
    });

});
</script>
    
15.09.2015 / 13:23