Link to home
Start Free TrialLog in
Avatar of Shaji
Shaji

asked on

How to retrieve specific JSON records from a AJAX call

I am calling a REST based URL

http://localhost:8080/idt-ws/ver1.0/resources

and it returns the following data (only 2 records are shown for simplicity)

{  
   "status":"200",
   "records":[  
      {  
         "hostname":"db002.stage-example.com",
         "dbname":"idt_php_tst1",
         "resname":"untrusted",
         "resid":"6"
      },
      {  
         "hostname":"db001.test-example.com",
         "dbname":"idt_java",
         "resname":"trusted",
         "resid":"9"
      }
   ]
}

Open in new window


I am constructing a data table from the records. How to pass specifically the records in the place of the URL. As I understand it is expecting the URL instead of JSON data at line number 12

$(document).ready(function() {
$('#btn-enable').hide();
var baseURL = "http://localhost:8080/idt-ws/ver1.0";
var path = "/resources";
var requestURL = baseURL + path;

$.getJSON(requestURL, function(data) {
var table = $('#idt-table').DataTable( {
	"scrollY": "300px",
        "scrollCollapse": true,
        "ajax": {
            "url": data.records,
            "dataSrc": ""
        },
        "columns": [
            { "data": "hostname" },
            { "data": "dbname" },
            { "data": "resname" },
            { "data": "resid" }
        ]	
    } );
});

Open in new window


I created a separate JSON file viz. 'data.json' and replaced it in line number 12, as shown above and it worked, but giving the array 'data.records' in the URL is not working.

Please help
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

I think what you are looking is a way to filter JSON result.

You can look at the blog post @, which explains how to use datafilter with jQuery Ajax calls.


https://community.optimizely.com/t5/Developers/Using-jQuery-ajax-datafilter-to-modify-responses-before-success/td-p/11165
ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shaji
Shaji

ASKER

Thanks Rainer, it is working  like a champ