I have developed a website for a local radio station and they asked if it was possible to track the number of times the podcasts were either played or downloaded. I am using the mp3 Browser Fork plugin (https://extensions.joomla.org/extensions/extension/multimedia/multimedia-players/mp3-browser-fork/) to generate the podcast pages, so I added code to the MP3 Browser Fork plugin to do an Ajax post to a new plugin I created to record the clicked event in a database table.
This code worked fine on my Localhost environment (see specs at the bottom of this post), but when I migrated the coding changes to my Development sub-domain the transactions were not being recorded.
I simplified the code down to Ajax posting a string and the PHP function immediately returning a string value. The Post returns a 200 OK and the Ajax post appears to be successful, but the PHP function is never being executed, thus the Response Body shows an empty data string.
I replicated this on my localhost environment by misspelling the plugin name in the Ajax post URL. Using the Eclipse IDE, and Microsoft Edge's F12 Developer Tools I was able to confirm that the POST was successful, but the PHP function did not execute.
Here is what I have tried so far:
- Downloaded Development site to localhost to verify nothing was missed during migration
- Simplified code
- Compared URLs in the POST header to verify they match between localhost and the development sub-domain
- Hard coded exact URL for the development sub-domain
- Renamed .htaccess file (in both sub-domain and production domain) to eliminate any re-mapping issues
- Turned off SEO-Search Engine Friendly URLs
- Changed the name of the function so it does not match the plugin name
- Verified PHP post_max_size value (64MB)
- Downgraded PHP version to 5.4
- Turned on all error reporting and logging (on localhost with misspelled plugin name - didn't see anything that the F12 Developer Tools didn't show me)
- Compared Microsoft Edge's F12 Developer Tools information with Google Chrome's Debugging tools (same information on both)
- Made coding changes to the production environment (eliminates the Sub-Domain as the issue)
None of these things allowed the PHP function to execute. My belief is that there is something in the hosted environment that is not allowing the Ajax Post to locate the PHP function, but since the post is getting a successful response from the server, no error message is being generated.
Here is all of the applicable code and responses:
Template modified to include JQuery Library
<head>
<?php echo $this['template']->render('head'); ?>
<script src="http://content.jwplatform.com/libraries/3sVcGVbK.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
PHP Plugin (first function)
<?php
// No direct access
defined('_JEXEC') or die;
// Import library dependencies
jimport('joomla.plugin.plugin');
class plgAjaxPodcastTracker extends JPlugin
{
Public function onAjaxPodcastTracker()
{
return "Test";
}
Plugin XML file:
<?xml version="1.0" encoding="utf-8"?>
<extension version="3.7"
type="plugin"
group="ajax"
method="upgrade">
<name>Ajax - PodcastTracker</name>
<version>0.1</version>
<creationDate>July 28, 2017</creationDate>
<author>Brian Benedum</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.readyfm.com</authorUrl>
<license>GNU General Public License version 2 or later</license>
<copyright>Copyright (C) 2017 All rights reserved.</copyright>
<description>Joomla Ajax Plugin</description>
<files>
<filename plugin="PodcastTracker">podcasttracker.php</filename>
</files>
</extension>
New function added to the Configuration.php file of the MP3 Browser Fork plugin
public function getDownloadTrackerscript() {
$format = "<script type=\"text/javascript\">";
$format .= "$(document).ready(function(){";
$format .= "$(\"#download_%2\").click(function(){";
$format .= "$.ajax({";
$format .= "type: \"POST\",";
$format .= 'data: {"data": "mydata"},';
$format .= "url: \"index.php?option=com_ajax&plugin=PodcastTracker&method=PodcastTracker&format=json\",";
$format .= "success: function(results) {";
$format .= "console.log(results);";
$format .= "}";
$format .= "});";
$format .= "});";
$format .= "});";
$format .= "</script>";
return $this->get("DownloadTrackerscript", $format);
}
The Headers, Request and Response Body
Localhost and Hosted environments
LOCALHOST:
Web Server: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP 5.6.23 Joomla: 3.7.4
Development Sub-Domain & Production Domain:
Hosting Provider: Bluehost
Web Server: Apache 2.2.32 PHP 7.0.12
Thank you in advance for any insights. I'm fresh out of ideas!