I use JQuery AJAX to retrieve some data (title and description). As you can see I get a json array and loop through the results and out the results in a div tag
success : function(jsonArray)
{
$.each(jsonArray,function(messageIndex,jsonObject)
{
$("#results").append(jsonObject.title + " " + jsonObject.description);
})
}
The JSONArray contains JSON Objects like so:
title : test Title 1
description : test Description 1
title : test Title 2
description : test Description 2
The JSONArray is created by a backend application I have and then the results passed to the client (JQuery AJAX). The backend is also responsible for displaying a button if it can. How can I let the client know whether to show the button or not?
I was thinking of having another JSONObject which has details about the button to show.
For example the JSONObject would be something like
showButton: false
colour : red
size : 50px
The problem is that I’m not sure how to represent this in my JQuery AJAX success. Currently all I’m given is a JSONArray that loops through the objects but how will it know that it hits the button json object?
Do I need to make another AJAX call to get the object?
Your current object structure is something like:
Instead you can have your backend return a structure something like this:
Then in your callback function:
(Note that JSON is a string representation used to send the data, but once it’s been parsed and you start using it in your success function it is not JSON or a “JSON object” or “JSON array”, it is just an object or array.)