0

Goal:to display a table populated with data from a json file. within the same function displayData, i want to parse the json file but outside the ajax call. I hope that it does make sense!

i set up an ajax call (which is working):

function displayData(term){
var frmStr =$('#input1').serialize();                                                                                                             

$.ajax({                                                                                                                                                  
        url:'./cgi_tableData.cgi',                                                                                                                          
        dataType:'json',                                                                                                                                  
        data: frmStr,                                                                                                                                     
        success: function(data, textStatus, jqXHR){                                                                                                       
            alert("success"),  
           //how to send the data outside this ajax call within the same function                                                                                                                           

        },                                                                                                                                                
        error: function(jqXHR, textStatus, errorThrown){                                                                                                  
            alert("Failed to perform gene search! textStatus: (" + textStatus +                                                                           
                  ") and errorThrown: (" + errorThrown + ")");                                                                                            
        }                                                                                                                                                 
    });   

............                                                                                                                                                

the question is how do I extract the json file from the ajax call so i can parse the file. I realize that it has to be initiated in success but i can't figure it out? once the data is extracted i will rearrange them in arrays to fit the table constructor.

ApisMel
  • 67
  • 1
  • 10
  • maybe i am wrong but here i don't want to return the data to a different function i want to keep the data from the json file within the same function (displayData). here why (i am new to javascript): i created a new table object after the user click a button. the table constructor is expected to receive the dataSet. when I hard code the dataSet in displayTable() and return the dataset it is working great. so now instead of hardcoding the dataset I want the data from a json file. but as it is now i am not able to extrat the data and return it to the constructor – ApisMel Apr 22 '18 at 21:36
  • i tried to add within success: var result = $.parseJSON(data); – ApisMel Apr 22 '18 at 21:46
  • No need to use `$.parseJSON()` since you set `dataType:'json'`. That means `data` in success callback will already be parsed as array or object assuming the json sent is valid – charlietfl Apr 22 '18 at 23:00

0 Answers0