We help IT Professionals succeed at work.
Get Started

How to retrieve specific JSON records from a AJAX call

Shaji
Shaji asked
on
178 Views
Last Modified: 2015-11-08
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
CERTIFIED EXPERT
Most Valuable Expert 2013
Awarded 2012
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE