How to retrieve specific JSON records from a AJAX call

Shaji
Shaji used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Pravin AsarPrincipal Systems Engineer
Top Expert 2005

Commented:
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
Most Valuable Expert 2013
Awarded 2012
Commented:
Hi,

it might be more simplier than thougth. Just use the Ajax URL (which points to your REST service) and the "dataSrc" property which identifies the object name which contains the data array in the response:
$(document).ready(function() {
	$('#btn-enable').hide();
	var baseURL = "http://localhost:8080/idt-ws/ver1.0";
	var path = "/resources";
	var requestURL = baseURL + path;

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

Open in new window


HTH
Rainer

Author

Commented:
Thanks Rainer, it is working  like a champ

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial