Ajax call from the wire post button not working - JSON - Tidypics Plugin

I am following this ( http://learn.elgg.org/en/2.0/guides/ajax.html ) guide to get the album guid from tidypics. The action is executing correctly but I don't know where I am failing, whether it is on the javascript or the json_enconde in my action file. 

My Action file has this at the end but no exit though. I noticed that adding exit at the end causes this error "Unexpected error while performing an AJAX call. Maybe the connection to the server is lost." when you click on the button. 

//The album id value is retrieved correctly as I am logging it. 

echo json_encode(array('thewire_album_guid' => $album_guid));

 

My JavaScript looks like this.

 

document.getElementById('thewire-submit-button').onclick = function(e) {      

var album_guid = data['album_guid'];

 if (!album_guid) {

elgg.action('thewire/image/selectalbum', {

success: function(wrapper) {

if(wrapper.output) {

//This not returning any value. 

alert('album_guid: ' + wrapper.output.thewire_album_guid);

} else {

alert('the system prevented the action from running');

}

}

});

 

My javascript must be incorrect. 

  • Try adding forward() instead.
    Have you registered your action correctly? Does clicking a button actually send a request? JS would be best rewritten to an AMD module, and I don't know where you included it.

  • Ismayil,

    I found out what happened. Yes, the action was registered correctly and I overrode the security to the action by doing this. 

     elgg_register_action('thewire/image/selectalbum',"mod/my_plugin/thewire/image/selectalbum.php",'logged_in');
    
            elgg_register_plugin_hook_handler('public_pages','walled_garden','my_walled_garden_override');
    
    
    }
    
    function my_walled_garden_override($hook, $type, $pages) {
    
            $pages[] = 'action/thewire/image/selectalbum';
    
            return $pages;
    
    }
    
    

    This is what happened. I had echo messages displaying in a function that the action was using to troubleshoot other things. The echo messages were the culprit. I figured out by just doing alert(wrapper.output), I saw the other echo messages in the alert as well a my json_enconde message. 

    Moral of the story: If you're going in circles with json_enconding, make sure you don't have any other echo messages in the same action except 

    echo json_encode(array('your_identifier' => $the_value_you_want_to_return));

    I hope this comes in handy to anyone.

  • You don't need to use walled garden hooks for actions: just register them as 'public'.
    Glad you figured it out

  • Without knowing what you want to achive: do logged-out users even need to be able to execute the action? If not, you wouldn't need to register the action as "public" (logged in users is default) and as Ismayil said, it's also not necessary to register this action as "public" for walled garden.

  • Thank you guys for your suggestions. I changed that and it work without having to do the walled garden override. Since the Tidypics plugin is doing it like that I was following it. Perhaps the developer of the plugin had his/her reasons to do it in such way. 

  • I had noticed that you might have used the "selectalbum" ajax view of Tidypics as an example. But this view is NOT registered as a public page but only registered as ajax view:

    elgg_register_ajax_view('photos/selectalbum');

    The usage of the 'public_pages', 'walled_garden' plugin hook in Tidypics is for other reasons. The actions registered are connected with image uploading and in case the site does this with Flash it's necessary to register the actions involved as public_pages because the Flash uploader does not use the session cookies.

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking