Get data from responseJSON

I cant use the exact code here, but I am running into an issue. I have an api call that looks like

getApi: function(){
return $.ajax({
type: “GET”
url: api url
dataType: ‘json’
})

}

Then I have a function that creates a table with the data from the api. I am using a jQuery datatable and it shows that 5 of 20 items are showing, but the items are not showing up. Its like they are there, but you cant see them. When I look in the console I can see the results in responseJSON. Using jquery and ajax is there a way to get to that responseJSON?

It’s hared to tell without seeing your code.

Remember that $.ajax returns a Promise, so it has to be handled. There are two main ways to do this:

const func1 = () => {
  $.ajax({ url: 'https://jsonplaceholder.typicode.com/posts/1' })
    .then(data => console.log('data 1', data))
}

const func2 = async () => {
  const data = await $.ajax({ url: 'https://jsonplaceholder.typicode.com/posts/1' })
  console.log('data 2', data)
}

func1()
func2()

Here is a pen for you to try it out.

1 Like

In all fairness, there may be other ways to do this - I haven’t used jQ in a very long time.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.