Harrison Reeves
Harrison Reeves

Reputation: 101

Why can I not parse my config.json file in JavaScript using RequireJS?

I have no idea why I'm not able to parse this JSON file in JS using RequireJS.

var settings = require('https://api.jsonbin.io/b/5e9535d3634f8d782606be53');
var parsed = JSON.parse(settings);
var indexPar = document.createElement('p');

window.onload = testJSON();

function testJSON() {
    indexPar.innerHTML = "Full name is " + parsed.fname + " " + parsed.lname + "\nExpiration Date is " + parsed.expdate + "\nCredit Card # is " + parsed.creditcardnumber + "\nCVV is " + parsed.cvv;
}
<script type="text/javascript" src="https://requirejs.org/docs/release/2.3.6/r.js"></script>

To my knowledge, it should show this on the main HTML page:

Full name is Harrison Reeves

Expiration Date is 01/23

Credit Card # is 1234 5678 9012

CVV is 000

Upvotes: 0

Views: 66

Answers (1)

Sandesh Mankar
Sandesh Mankar

Reputation: 699

Check this out. Used this $.getJSON function to fetch a JSON file.

$.getJSON( "https://api.jsonbin.io/b/5e9535d3634f8d782606be53", function( parsed ) {
    window.onload = testJSON();

function testJSON() {
   $("#list").html("Full name is " + parsed.fname + " " + parsed.lname + "\nExpiration Date is " + parsed.expdate + "\nCredit Card # is " + parsed.creditcardnumber + "\nCVV is " + parsed.cvv);
}
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

<pre id="list"></pre>

Upvotes: 1

Related Questions