I have a getJSON request that goes to a PHP page on my root (htdocs) directory of my wordpress site. It should make the JSON request and return the value to append the div on the Wordpress page. I am using Scripts and Styles plugin to add the javascript in the body of an individual page.
When I test just the script on my local host (without Wordpress) it works fine. However, when I add it to Wordpress page and try to run the script, I get the following error:
"Uncaught TypeError: Cannot read property 'events' of null"
Here is my script. I put eventbrite_api2.php in the root directory (htdocs) of the server. Not sure if this is just a path issue or something else, would appreciate assistance.
jQuery.getJSON('eventbrite_api2.php', {eventid: '14900532895', function:'next'},
function (response) {
var upcoming = response; //we're naming the response 'upcoming' for easier reading
console.log(upcoming.events[0]); // this is for testing, comment this out when not needed
var date = new Date(upcoming.events[0].start.utc);//this formats the date to local time zone
console.log(date); //this prints the next date, for testing..
event_start = date.toLocaleString('en-US', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric', hour: 'numeric', minute: 'numeric', hour12: true, timeZone: "America/New_York" });//this formats the date to eastern time zone, and makes some other formatting of the date
var content = "<h4>" + upcoming.events[0].name.text + "</h4><p>Next Tour: " + event_start + "</p>" ;
$("#eventbrite").append(content);
});
/
at the beginning so it'll always be relative. But why are you using standalone PHP files in a WP site to handle requests? Is there a reason WP AJAX or the REST API aren't options? Keep in mind that by joining up variables and HTML in JS you're opening yourself up to injection attacks,jQuery( '<h4>', { 'text': ... } )
is just as simple but 100x more secure