Title: Trying to run a console log on php file storing json but callback error and 404 (not found)
Tags: javascript;php;html
Question: Working with trying to learn json and ajax and how they interoperate with html and javascript
I have a php with json data inside
I am trying to get the json data formatted into the html page but I keep getting error that "callback is not a function"
I am running the php and html files on my MAMP server to simulate a api feed
I will share my html and js files
<html lang="en"&gt;
<head&gt;
<meta charset="UTF-8"&gt;
<meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
<link href="style.css" rel="stylesheet" type="text/css"&gt;
<script src="run2.js"&gt;</script&gt;
<title&gt;Ajax Demo</title&gt;
</head&gt;
<body&gt;
<h1 class="title"&gt;Todays Weather Forecast</h1&gt;
<p class="sub"&gt;Click the button the check the local weather.</p&gt;
<button class="demo-centered" type="button" onclick="loadPhp()"&gt;Check Weather</button&gt;<br&gt;<br&gt;
<p id="demo"&gt;</p&gt;
</body&gt;
</html&gt;
var loadPhp = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function () {
var status = xhr.status;
if (status == 200) {
callback(null, xhr.response);
} else {
callback(status);
xhr.send();
loadPhp('demo.php', function (err, data) {
if (err != null) {
alert('Something went wrong: ' + err);
} else {
for (var i = 0; i < data.length; i++) {
for (x in data[i]) {
console.log(data[i][x]);
}
PHP just in case
{"coord":{"lon":-116.8,"lat":33.03},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"stations","main":{"temp":293.73,"feels_like":289.89,"temp_min":289.26,"temp_max":295.93,"pressure":1016,"humidity":52},"visibility":16093,"wind":{"speed":5.7,"deg":260},"clouds":{"all":40},"dt":1589408840,"sys":{"type":1,"id":5686,"country":"US","sunrise":1589374130,"sunset":1589423903},"timezone":-25200,"id":5391832,"name":"San Diego County","cod":200}
Comment: How would I go about getting something like just the temp and the description only on an html page?


Here is another answer: You have to create a javascript function called callback to do what the you want the callback to do.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/17/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><php><html>', 'Title': 'Trying to run a console log on php file storing json but callback error and 404 (not found)', 'ViewCount': 22, 'id': 61848951, 'user_AboutMe': '', 'user_AccountId': 15795617.0, 'user_DisplayName': 'DevOps_N_Training', 'user_Reputation': 83.0, 'user_Views': 45.0, 'user_id': 11397523.0}}