0

I want to modify json object inside a function and then use the received value at another function. This is as a glimpse what I've coded:

$(document).ready(function () {

    var json

    $("#ok").click(function(){

        function plotReglaFalsa(respuesta) {
            json = respuesta
        }

        ....
        ....
        plotReglaFalsa(anyJSON)

        function populateTable () {
            console.log(json)
        }

        populateTable()

    })

However json is not being modified, I do call functions required to the to the point of json being modified and the console.log statement prints undefined. I also know respuesta is a valid object.

However, plotReglaFalsa is actually a callback argument in an AJAX request, this is my full code:

$(document).ready(function () {

    var json

    $.plot($("#placeholder"), [ [] ], { yaxis: { show: true, max: 100, min: -100 }, xaxis: { show: true, max: 100, min: -100} });

    $("#ok").click(function(){

        var args = {    algorithm: 'ReglaFalsa.py',
                        parameters: "\'"+$("#fx").val()  + "\' "  +
                                    $("#a").val()   + " "   +
                                    $("#b").val()   + " "   +
                                    $("#tolerancia").val() + " " +
                                    $("#iteracionesMaximas").val()
                    }

        function plotReglaFalsa(respuesta) {

            json = respuesta
            var result = []
            for (var series in respuesta) {

                if (series!="x" && series != "y")      
                result.push({    
                    data : [ [respuesta[series].a,respuesta[series].F], [respuesta[series].b, respuesta[series].G]]
                })
            }

            result.push({
            label: "fx",
            color: "#FB2365",
            data: _.zip(respuesta['x'], respuesta['y'])
            })


            var plot = $.plot(  $("#placeholder"), 
                                result, 
                                {   selection:{mode: "xy"},  
                                    zoom: { interactive: true }, 
                                    pan: { interactive: true }, 
                                    grid: { markings: [{ xaxis: { from: 0.0, to: 0.0 }, color: 'black', lineWidth: 1 }, { yaxis: { from: 0.0, to: 0.0 }, color: 'black', lineWidth: 1 }] }
                                })
            plot.getOptions().selection.mode = null
        }

        getSendingJSON("/plot",args,plotReglaFalsa)
        populateTable()

        function populateTable () {
            console.log(json)
            $("body").append("<table id='resultados' class='table table-bordered'><thead><th>i</th><th>a</th><th>F</th><th>b</th><th>G</th><th>w</th></thead><tbody></tbody></table>")

            $('#resultados').dynatable({

                features: {
                    paginate: false,
                    sort: false,
                    search: false,
                    perPageSelect: false,
                    recordCount: false
                },
                dataset: { records: json }
            })
        }        


    })
})

And this is populate table function:

function getSendingJSON(url,reqObject,callBack)  {
        $.ajax({
            type: "get",
            data: reqObject,
            dataType: "json",
            url: url,
            success: function(response){
               callBack(response);
            }
        });
    }

Understanding that the issue is around the AJAX nature, what's the easiest workaround to assign response to a variable?

diegoaguilar
  • 8,179
  • 14
  • 80
  • 129

0 Answers0