I'm trying to learn JS and have limited experience with JAVA. I am looking to (in the very long term) develop a web app for helping my students with introductory physics homework. In the first steps towards creating this app, I've put some basic equations (as TeX formatted strings to later show as pleasantly formatted equations)and data in a .json file in the same folder as the html file that will call my .js file to execute some functions.
I have searched for a bit on how to upload/read json files in javascript but most articles seem to discuss using AJAX or jquery (I'm unfamiliar with these and the $.function() calls have me confused). Most others are geared towards loading a local file in the sense that the js is server side and the json data is client side. Can anyone point me in the direction of useful code for my application? I want to keep the equation data in a separate file to leave it easily modifiable and I plan on having similar json formatted data for the app to import an array of selectable physics concepts as well.
An example of the JSON data looks like this and is stored in a file in the same folder called 'equations.json'
{
"equationGroup": [{
"equationGroupName": "Kinematics",
"equation": [{
"equationName": "Final Velocity Squared",
"equationTeX": "v_{f}^{2}=v_{i}^{2}+2a(x-x_{0})",
"equationVariables": [{
"variableTeX": "v_{f}",
"variableDescription": "The final velocity"
},
{
"variableTeX": "v_{0}",
"variableDescription": "The starting velocity"
},
{
"variableTeX": "a",
"variableDescription": "Acceleration in the same plane as velocity\""
},
{
"variableTeX": "x",
"variableDescription": "The final displacement"
},
{
"variableTeX": "x_{0}",
"variableDescription": "The starting point (initial displacement)"
}
]
},
{
"equationName": "displacement",
"equationTeX": "x_{f}=x_{0}+v_{0}t+\\frac{1}{2}at^{2}",
"equationVariables": [{
"variableTeX": "x_{f}",
"variableDescription": "The final displacement"
},
{
"variableTeX": "x_{0}",
"variableDescription": "The initial displacement (starting point)"
},
{
"variableTeX": "v_{0}",
"variableDescription": "The initial velocity"
},
{
"variableTeX": "t",
"variableDescription": "The time elapsed"
},
{
"variableTeX": "a",
"variableDescription": "acceleration"
}
]
}
]
}
Here is the HTML I have so far:
<!DOCTYPE html PUBLIC "testPage">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Testing javscript stuff</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-MML-AM_CHTML' async></script> <!-- call for TeX reader -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Call for JSON getter -->
</head>
<body>
\[v_{f}^{2}=v_{i}^{2}+2a(x-x_{0})\]
<script src="SECAPS.js"></script>
</body>
</html>
And the SECAPS.js file is here
var equations ={};
$.getJSON('equations.json', function(data) {
equations=data;
});
document.write("The equation group name is:" + equations.equationGroup[0].equationGroupName);