2

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

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!

1 Answer 1

1

The correct call is:

index.php?option=com_ajax&plugin=PodcastTracker&method=PodcastTracker&format=json

Plugin and method names without the "plgAjax" and "onAjax". The plugin must be installed in the ajax group.

Example plugin: https://github.com/Joomla-Ajax-Interface/Ajax-Latest-Articles

Ajax Interface doc: https://docs.joomla.org/Using_Joomla_Ajax_Interface

Test script for example plugin:

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<button id="download">Test</button>
<?php 

 $format = "<script type=\"text/javascript\">";
    $format .= "$(document).ready(function(){";
    $format .= "$(\"#download\").click(function(){";
    $format .= "$.ajax({";    
    $format .= "type: \"POST\",";      
    $format .= 'data: {"data": "mydata"},';      
    $format .= "url: \"index.php?option=com_ajax&plugin=Latestarticles&method=Latestarticles&format=json\",";          
    $format .= "success: function(results) {";           
    $format .= "console.log(results);";            
    $format .= "}";
    $format .= "});"; 
    $format .= "});";  
    $format .= "});";
    $format .= "</script>";

    echo $format;

    ?>
4
  • Thank you for your response. I was referencing the links you provided, but when I could not get it to work on the hosted site, I started playing around with the plugin and method names. I forgot to remove the onAjax prefix from this code before I made this post. The Podcast Tracker plugin is installed as an ajax plugin (plugins->ajax->PodcastTracker), and I did verify that it is published. I've fixed the code, per your response, and re-tested it. The response array is still empty. I've included the updated Headers, Request Body and Response body results in the post.
    – bbenedum
    Commented Aug 8, 2017 at 11:20
  • Please download and install the Latest Articles plugin, activate it, use my run my test script. (min. 1 published article in Joomla is necessary for a result) When this works, please add the manifest file of your plugin to your question as well. Btw. check if com_ajax is activated. Commented Aug 8, 2017 at 13:43
  • Thanks to your help, I've figured it out. The Latest Articles plugin and test script worked, so I took a closer look at the plugin xml file (which I included in the post). I noticed that the filename tag values were all lowercase, so I changed plugin="PodcastTracker" to plugin="podcasttracker". Now it works. Not sure why, since I was consistent in labeling everything, but as long as it's working I'm happy. Thanks again!
    – bbenedum
    Commented Aug 8, 2017 at 16:25
  • You are welcome, good luck. Commented Aug 9, 2017 at 7:55

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.