Ajax request parameter being a variable

Asked

Viewed 98 times

0

Hello, I have a little doubt 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);
        }
    });
});

});

note that the "type" parameter comes from the "name" attribute of an input (User has some inputs in a table, and depending on what they type in each input, they search in the database according to this table column) But when I pass "type" in ajax, it treats this "type" as the name of the request, and not the value it has (it can be "responsible/subject/date/etc") what can be done? thank you

1 answer

2


When you create a literal object like {tipo: text}, the keys never are variable. Therefore 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 bracket notation:

var obj = {};
obj[tipo] = text;
  • Putz, truth! I had forgotten that, thank you, solved my problem!

Browser other questions tagged

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