imgrooot Posted November 14, 2020 Share Posted November 14, 2020 (edited) I am retrieving data from a third party's API using AJAX method. I would like to do two things. 1. Display the data records on a page. 2. Create a pagination on the page to display records more efficiently. I am expecting to retrieve possibly hundreds of records. I can normally do this using PHP but since I am retrieving the records using AJAX, the pagination is gonna be a challenge. First things first is to display the records. Here is my AJAX code. It displays the log data fine. But it doesn't display any data in the "output" div. And it gives this error in the console. Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) at Object.success <div class="output"></div> <script> $.ajax ({ type: 'GET', url: "https://3rdpartywebsite.com/api/GetCustomers", dataType: 'json', processData: false, contentType: false, success: function(data) { console.log(data) $newData = JSON.parse(data); $.each($newData, function(i, v) { $('.output').append(v.LastName); $('.output').append(v.FirstName); }); } }); </script> What am I doing wrong? Edited November 14, 2020 by imgrooot Quote Link to comment Share on other sites More sharing options...
Barand Posted November 14, 2020 Share Posted November 14, 2020 $newData = JSON.parse(data); Try commenting out the above line - it shouldn't be necessary as you specified a data-type: json Quote Link to comment Share on other sites More sharing options...
imgrooot Posted November 14, 2020 Author Share Posted November 14, 2020 6 hours ago, Barand said: $newData = JSON.parse(data); Try commenting out the above line - it shouldn't be necessary as you specified a data-type: json I did that. No more error but nothing is displaying in the .output div. $.each(data, function(i, v) { $('.output').append(v.LastName); }); Quote Link to comment Share on other sites More sharing options...
imgrooot Posted November 14, 2020 Author Share Posted November 14, 2020 I have a solution. Apparently I was inputting the wrong label name. Instead of it being "FirstName", it's actually with lowercase "firstName". Here's the new code. <script> $.ajax ({ type: 'GET', url: "https://3rdpartywebsite.com/api/GetCustomers", dataType: 'json', success: function(data) { $.each(data, function(i, v) { $('.output').append(v.firstName); $('.output').append(v.lastName); }); } }); </script> // To access individual record inside the array. console.log(data[0].firstName); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.