Jquery Ajax Response

1

This is my code

controller =  $("#src_controller").val();
source_id =  '#' + $("#src_filter_tags").val();
$.ajax({
    type: "GET",
    data:"id=" + $(this).data('tagId'),
    dataType: "json",
    url: "index.php?controller=" + controller + "&action=FindByTag",
    success: function(response){
        var resultArray = [];
        $.each(response, function(k, v) {
            resultArray.push(v.*);
        });
        console.log('#' + $("#src_filter_tags").val());
        $(source_id).val(resultArray);
        return 1;
    },error: function() {
        console.log("error");
    }
});

What I want is on the line     resultArray.push (v. *); the asterisk is the name of the field that I bring from the database.

I want it to change dynamically, that is, assign a variable to that asterisk so that with each iteration of each I take a different variable.

Another solution would be to get the first value of what I get for JSON.

The problem is that depending on which table in my database asks the fields ids are different.

For example

Table1 = > table_id1 Table2 = > table_id2

So what I want to do is variable = table_id1 or table_id2 resultArray.push (v.variable);

I've managed to do it like this:

$.each(response, function(k, v) {
    $.each(v, function( k, v ) {
        resultArray.push(v);
        return false;
    });
});

I want to do this in a cleaner way

    
asked by Subiendo 25.03.2018 в 15:48
source

1 answer

1

We should see what the server code that generates the response is, but what I would do would be to "normalize" the field names so that the client code does not have to decide anything.

That is, regardless of which table in the database it is, send the answers always with the same field names.

    
answered by 29.03.2018 в 04:01