I am trying - for the first time - to connect to a Blackbaud API, retrieve a JSON object, and display it on a web page. The request.open link I am using (not shown below because it contains user data) returns information with no error when I put it directly into browser.
An example of the JSON data return is here:
{"getConsTransactionsResponse":{"transaction":{"amount":{"formatted":"$50.00","decimal":"50.00"},"timestamp":"2016-02-01T09:43:54.458-06:00","pay_method":"ONLINE","billing_name":{"title":null,"last":"Smith","suffix":null,"middle":null,"first":"John","prof_suffix":null},"transaction_type":"DONATION","tender_type":"INVALID","confirmation_code":{},"donation_detail":{"value_of_goods":{"formatted":"$0.00","decimal":"0.00"},"tax_deductible_amount":{"formatted":"$0.00","decimal":"0.00"},"level":{"id":"9921","title":"API Donation Level"},"form":{"id":"9999","title":"API Form","description":"Sample form for API documentation."},"campaign":{"id":"9901","title":"API Campaign","description":"Sample campaign for API documentation."}},"billing_address":{"street2":null,"zip":null,"street1":null,"county":null,"state":null,"street3":null,"country":null,"city":null}}}}
The script I am using to retrieve and then display the data is here:
<script type="text/javascript">
$(document).ready(function() {
"use strict";
var donHistory = "";
var request = new XMLHttpRequest();
request.open('GET', 'JSON LINK', true);
request.onload = function () {
// begin accessing JSON data here
var data = JSON.parse(this.response);
donHistory += '<div>';
for (var i=0; i<data.length; i++) {
donHistory += data[i].transaction.amount + '<br>';
}
donHistory += '</div>';
var divHistory = document.createElement("div");
var divHistoryText = document.createTextNode(donHistory);
divHistory.appendChild(divHistoryText);
var divElement = document.getElementById("col-content-box");
divElement.appendChild(divHistory);
};
request.send();
});
What gets displayed on my web page is just an opening and closing div.
It's placed in the right area, just with no JSON data. The console shows no errors. Obviously, I am new to this and am making some simple mistake.
What I would like help with is:
1) Why isn't the JSON data which I know exists not making its way to the screen?
2) If/when I do get this working, what am I doing wrong to make the display as text instead of embedded as code?
$('<div></div>').text(donHistory).appendTo(divElement)