Forums / Developer / Ajax load of a form (content/action)

Ajax load of a form (content/action)

Author Message

Stéphane Le Merre

Thursday 22 July 2010 10:14:22 am

Hello,

I need to load the form to post comments on eZPublish articles : to do that, i'm using jquery framework. I'm always getting 404 errors when calling content/action with Ajax.

I'm using an empty layout, and try to load the form by making a POST query : something like this :

function loadAjaxContent(_url,_el,_params,_method)
    {
        var params = (_params==null)? {} : _params;
        var method = (_method==null)? 'post' : _method;

        $.ajax({
           type: method,
           url: _url,
           data: params,
           dataType: "html",
           cache: false,
           success: function(response){
             $('#'+_el).html(response);
           },
            error:function (xhr, ajaxOptions, thrownError){
                $('#'+_el).html("<p>Erreur lors du chargement : "+xhr.status+"</p>");
            }    
         });
    }

In the tpl file :

function newComment()
 {ldelim}
      var _params = 
      {ldelim}
             ClassIdentifier:"comment",
             NodeID:"{$node.object.main_node.node_id}",
             ContentLanguageCode:"{ezini( 'RegionalSettings', 'ContentObjectLocale', 'site.ini')}"
      {rdelim};
      loadAjaxContent({"layout/set/empty/content/action"|ezurl()},'new_comment',_params);
      return false;
{rdelim}

I'm simply passing the expected parameters to generate the template , note :

  • I do not get errors when i try to display my form with empty layout in navigator,
  • Ajax function is working : i can display some other parts that do not need to be compute by action view.

Am I missing something ?

P-S : Maybe it has an importance, this website is running ez4.0

http://www.ligams.com

Gaetano Giunta

Thursday 22 July 2010 2:53:49 pm

What about using firebug to debug the xhr request? It will tell you if the url that you're posting to is correct, as well as the request's payload.

In the example you posted I think you might be missing some parameters to post to content/action.

Principal Consultant International Business
Member of the Community Project Board

Ivo Lukac

Friday 23 July 2010 1:05:25 am

I think the problem could be in a fact that if you want to publish some content you need to have a draft already created.

Do you create a draft version of the object prior to publish?

http://www.linkedin.com/in/ivolukac
http://www.netgen.hr/eng/blog
http://twitter.com/ilukac

Stéphane Le Merre

Friday 23 July 2010 6:57:41 am

Thanks for answering. I found out how to solve this by watching kernel/content/action.php, eZ uses the submit button name to control :

if ( $http->hasPostVariable( 'NewButton' ))
//...

So we must had a post param call NewButton in my case :

function newComment()
{ldelim}
    var _params = 
    {ldelim}
            ClassIdentifier:"comment",
            NodeID:"{$node.object.main_node.node_id}",
            ContentLanguageCode:"{ezini( 'RegionalSettings', 'ContentObjectLocale', 'site.ini')}",
            NewButton:"Ajouter%20un%20commentaire"
    {rdelim};
    loadAjaxContent({"layout/set/ajax/content/action"|ezurl()},'new_comment',_params);
    return false;
{rdelim}

And it worked.

Thanks Gaetano :)

http://www.ligams.com

Andy Caiger

Monday 25 April 2011 1:37:50 am

Sounds interesting...

I'd love to see the complete solution for this!

EAB - Integrated Internet Success
Offices in England, France & China.
http://www.eab.co.uk http://www.eab-china.com http://www.eab-france.com