I need to load a url and have the only text on this page be a JSON string.
As an example I built a simple function to get the day of the week: getDayOfWeek()
. My actual function is much more complex, and cannot easily be converted to another language.
But I cannot simply load this into the DOM. I need the only text on the page to be the JSON object.
For example:
http://example.com/what_day_is_it // needs to return: { "day": "Friday" }
Here's a fiddle: http://jsfiddle.net/7b49Lorw/
HTML:
<span id="day"></span>
JS:
function getDayOfWeek() {
var d = new Date();
d.getDay();
var gsDayNames = new Array(
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
);
return gsDayNames[d.getDay()];
}
$('#day').text(getDayOfWeek());
Assuming I use PHP for the server side, how can I use PHP to evaluate the result of a JS function and return the JSON string?