0

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); 
});
3
  • 1
    Looks like a path issue, that path has no / 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
    – Tom J Nowell
    Commented Nov 27, 2017 at 18:05
  • Thanks Tom. I am calling a REST API from a third party service provider (Eventbrite). When I do this on my localhost without Wordpress it works fine. I understand Wordpress has some additional issues that I need to translate properly.. but just trying to see if there's a basic way to get this script to work on Wordpress page without reinventing the wheel.
    – Daniel C
    Commented Nov 27, 2017 at 18:43
  • Check the network console see what the response code for this file when you do this request.
    – Shibi
    Commented Nov 27, 2017 at 18:51

1 Answer 1

-1

Turns out the issue was not with Wordpress but with the PHP Configuration. allow_url_fopen was set to off on the server, whereas on the localhost it was on. When I updated that, the script worked fine. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.