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
ShajiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pravin AsarPrincipal Systems EngineerCommented:
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
0
Rainer JeschorCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShajiAuthor Commented:
Thanks Rainer, it is working  like a champ
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.