Ajax request parameter being a variable

0

Hello, I have a small question on how to do this:

$(function(){
$("input").keyup(function(){
    var tipo = $(this).attr("name");
    var text = $(this).val();

    $.ajax({
        url: "minhaurl",
        data: {tipo: text},
        type: "POST",
        success: function(result){
            $("#replaceResultSearch").html(result);
        }
    });
});

});

Notice that the "type" parameter comes from the "name" attribute of an input (The user has some inputs in a table, and depending on what he types in each input, he searches the database for that column of the table) But when I pass "type" into ajax, it treats this "type" as the name of the request, not the value it has (can it be "responsible / subject / data / etc") what can be done? Thank you

    
asked by anonymous 07.07.2017 / 03:39

1 answer

2

When you create a literal object such as {tipo: text} , the never keys are variables. So your key is "type" itself, and this is being passed to the server. The only way to use a variable as a key is by bracketed notation:

var obj = {};
obj[tipo] = text;
    
07.07.2017 / 03:47