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
AJAXJSONJavaScriptjQuery

Avatar of undefined
Last Comment
Shaji

8/22/2022 - Mon
Pravin Asar

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
Rainer Jeschor

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Shaji

ASKER
Thanks Rainer, it is working  like a champ
Your help has saved me hundreds of hours of internet surfing.
fblack61