I have a json response I'm getting from a server with different response structures.
One uses another value "data" to store a link:
and the other doesn't (with "image" being the link):
I was wondering if there was a way I can get the "data.link" from the associative array with a reusable method, with linkVariableName being the key of the associative array.
function addLink(responseText, successVariableName, isError, linkVariableName)
{
var jsonResponse = JSON.parse(responseText);
var state;
if (isError)
state = !jsonResponse[successVariableName];
else
state = jsonResponse[successVariableName];
if (state) {
var link = jsonResponse[linkVariableName];
insertAtCursor(textArea, '[img]' + link + '[/img]\r\n');
} else
console.log('Error: ' + responseText);
}
so I can use either
addLink(response.responseText, 'success', false, 'data.link');
or
addLink(response.responseText, 'error', true, 'image');