Run function in com_content with ajax - doesn't call it corretly

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I've been following this tutorial to make an AJAX login form.
http://www.mysite4u.net/joomla-articles/jquery-ajax-joomla-login.html



But I can't get it to work. The ajax call dosen't work. It gives me this error Uncaught TypeError: Cannot read property 'loggedIn' of null



I tried reading this documentation, but I guess that it is not the AJAX component that is used in this, and it dosen't mention anything about calling components: https://docs.joomla.org/Using_Joomla_Ajax_Interface



This is my AJAX call:



form = jQuery('#userForm'); 

jQuery.ajax(
url: 'index.php?option=com_content&format=ajax&view=article&task=loginUser&tmpl=component',
type: 'post',
dataType: 'html',
data:
form: jQuery(form).serializeArray()
,
async: true,
success: function(response)
console.log(response);
var result = jQuery.parseJSON(response);

if ( result.loggedIn == 1 )
//User logged in

else
//User not logged in


);


And the function inside components/com_content/views/article/view.ajax.php
The "view.ajax.php" is a file I've created:



public function _logInUser( $form )
$options = array();
$credentials = array();

$credentials['username'] = $form[0]['value'];
$credentials['password'] = $form[1]['value'];

$result = JFactory::getApplication()->login($credentials, $options);
$result = ($result) ? 1 : 0;

//1 - logged in
//0 - not logged in
echo json_encode( array('loggedIn' => $result) );
jexit();











share|improve this question



























    up vote
    1
    down vote

    favorite












    I've been following this tutorial to make an AJAX login form.
    http://www.mysite4u.net/joomla-articles/jquery-ajax-joomla-login.html



    But I can't get it to work. The ajax call dosen't work. It gives me this error Uncaught TypeError: Cannot read property 'loggedIn' of null



    I tried reading this documentation, but I guess that it is not the AJAX component that is used in this, and it dosen't mention anything about calling components: https://docs.joomla.org/Using_Joomla_Ajax_Interface



    This is my AJAX call:



    form = jQuery('#userForm'); 

    jQuery.ajax(
    url: 'index.php?option=com_content&format=ajax&view=article&task=loginUser&tmpl=component',
    type: 'post',
    dataType: 'html',
    data:
    form: jQuery(form).serializeArray()
    ,
    async: true,
    success: function(response)
    console.log(response);
    var result = jQuery.parseJSON(response);

    if ( result.loggedIn == 1 )
    //User logged in

    else
    //User not logged in


    );


    And the function inside components/com_content/views/article/view.ajax.php
    The "view.ajax.php" is a file I've created:



    public function _logInUser( $form )
    $options = array();
    $credentials = array();

    $credentials['username'] = $form[0]['value'];
    $credentials['password'] = $form[1]['value'];

    $result = JFactory::getApplication()->login($credentials, $options);
    $result = ($result) ? 1 : 0;

    //1 - logged in
    //0 - not logged in
    echo json_encode( array('loggedIn' => $result) );
    jexit();











    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I've been following this tutorial to make an AJAX login form.
      http://www.mysite4u.net/joomla-articles/jquery-ajax-joomla-login.html



      But I can't get it to work. The ajax call dosen't work. It gives me this error Uncaught TypeError: Cannot read property 'loggedIn' of null



      I tried reading this documentation, but I guess that it is not the AJAX component that is used in this, and it dosen't mention anything about calling components: https://docs.joomla.org/Using_Joomla_Ajax_Interface



      This is my AJAX call:



      form = jQuery('#userForm'); 

      jQuery.ajax(
      url: 'index.php?option=com_content&format=ajax&view=article&task=loginUser&tmpl=component',
      type: 'post',
      dataType: 'html',
      data:
      form: jQuery(form).serializeArray()
      ,
      async: true,
      success: function(response)
      console.log(response);
      var result = jQuery.parseJSON(response);

      if ( result.loggedIn == 1 )
      //User logged in

      else
      //User not logged in


      );


      And the function inside components/com_content/views/article/view.ajax.php
      The "view.ajax.php" is a file I've created:



      public function _logInUser( $form )
      $options = array();
      $credentials = array();

      $credentials['username'] = $form[0]['value'];
      $credentials['password'] = $form[1]['value'];

      $result = JFactory::getApplication()->login($credentials, $options);
      $result = ($result) ? 1 : 0;

      //1 - logged in
      //0 - not logged in
      echo json_encode( array('loggedIn' => $result) );
      jexit();











      share|improve this question















      I've been following this tutorial to make an AJAX login form.
      http://www.mysite4u.net/joomla-articles/jquery-ajax-joomla-login.html



      But I can't get it to work. The ajax call dosen't work. It gives me this error Uncaught TypeError: Cannot read property 'loggedIn' of null



      I tried reading this documentation, but I guess that it is not the AJAX component that is used in this, and it dosen't mention anything about calling components: https://docs.joomla.org/Using_Joomla_Ajax_Interface



      This is my AJAX call:



      form = jQuery('#userForm'); 

      jQuery.ajax(
      url: 'index.php?option=com_content&format=ajax&view=article&task=loginUser&tmpl=component',
      type: 'post',
      dataType: 'html',
      data:
      form: jQuery(form).serializeArray()
      ,
      async: true,
      success: function(response)
      console.log(response);
      var result = jQuery.parseJSON(response);

      if ( result.loggedIn == 1 )
      //User logged in

      else
      //User not logged in


      );


      And the function inside components/com_content/views/article/view.ajax.php
      The "view.ajax.php" is a file I've created:



      public function _logInUser( $form )
      $options = array();
      $credentials = array();

      $credentials['username'] = $form[0]['value'];
      $credentials['password'] = $form[1]['value'];

      $result = JFactory::getApplication()->login($credentials, $options);
      $result = ($result) ? 1 : 0;

      //1 - logged in
      //0 - not logged in
      echo json_encode( array('loggedIn' => $result) );
      jexit();








      joomla-3.x ajax






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 30 mins ago

























      asked 1 hour ago









      JonasB

      1608




      1608




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          Apperently the tutorial was missing an important part. It needed to have the "loginUser" task addded to the switch in view.ajax.php like:



          function display($tpl = null)

          $task = JRequest::getString('task');
          switch( $task )
          case "registerNewUser":
          $this->_registerNewUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
          break;
          case "loginUser":
          $this->_logInUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
          break;

          jexit();






          share|improve this answer



























            up vote
            1
            down vote













            Maybe I misunderstand you in your question but in the directory of com_content/views/article you cannot/should not create a view.html.php file since it originally has a good view.html.php file.



            In the tutorial which you follow the author says that you should create an 'view.ajax.php' file in com_content/views/article folder (not a view.html.php file). Maybe that is causing your problem since you created a wrong file for this ajax call.



            And if that is just a mistake in your question, then you also might want to study this earlier answer about ajax calls within Joomla here (it is better not to repeat that here):
            What is the proper way to make an AJAX call in component?






            share|improve this answer






















            • Woops, my fault... It is a view.ajax.php I've created. Thanks for your suggestion!
              – JonasB
              29 mins ago










            • I added a link in my answer about the proper ajax calls in Joomla, if you did not study that before.
              – Zollie
              17 mins ago










            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "555"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            noCode: true, onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fjoomla.stackexchange.com%2fquestions%2f23583%2frun-function-in-com-content-with-ajax-doesnt-call-it-corretly%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            Apperently the tutorial was missing an important part. It needed to have the "loginUser" task addded to the switch in view.ajax.php like:



            function display($tpl = null)

            $task = JRequest::getString('task');
            switch( $task )
            case "registerNewUser":
            $this->_registerNewUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
            break;
            case "loginUser":
            $this->_logInUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
            break;

            jexit();






            share|improve this answer
























              up vote
              1
              down vote













              Apperently the tutorial was missing an important part. It needed to have the "loginUser" task addded to the switch in view.ajax.php like:



              function display($tpl = null)

              $task = JRequest::getString('task');
              switch( $task )
              case "registerNewUser":
              $this->_registerNewUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
              break;
              case "loginUser":
              $this->_logInUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
              break;

              jexit();






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                Apperently the tutorial was missing an important part. It needed to have the "loginUser" task addded to the switch in view.ajax.php like:



                function display($tpl = null)

                $task = JRequest::getString('task');
                switch( $task )
                case "registerNewUser":
                $this->_registerNewUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
                break;
                case "loginUser":
                $this->_logInUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
                break;

                jexit();






                share|improve this answer












                Apperently the tutorial was missing an important part. It needed to have the "loginUser" task addded to the switch in view.ajax.php like:



                function display($tpl = null)

                $task = JRequest::getString('task');
                switch( $task )
                case "registerNewUser":
                $this->_registerNewUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
                break;
                case "loginUser":
                $this->_logInUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
                break;

                jexit();







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 20 mins ago









                JonasB

                1608




                1608




















                    up vote
                    1
                    down vote













                    Maybe I misunderstand you in your question but in the directory of com_content/views/article you cannot/should not create a view.html.php file since it originally has a good view.html.php file.



                    In the tutorial which you follow the author says that you should create an 'view.ajax.php' file in com_content/views/article folder (not a view.html.php file). Maybe that is causing your problem since you created a wrong file for this ajax call.



                    And if that is just a mistake in your question, then you also might want to study this earlier answer about ajax calls within Joomla here (it is better not to repeat that here):
                    What is the proper way to make an AJAX call in component?






                    share|improve this answer






















                    • Woops, my fault... It is a view.ajax.php I've created. Thanks for your suggestion!
                      – JonasB
                      29 mins ago










                    • I added a link in my answer about the proper ajax calls in Joomla, if you did not study that before.
                      – Zollie
                      17 mins ago














                    up vote
                    1
                    down vote













                    Maybe I misunderstand you in your question but in the directory of com_content/views/article you cannot/should not create a view.html.php file since it originally has a good view.html.php file.



                    In the tutorial which you follow the author says that you should create an 'view.ajax.php' file in com_content/views/article folder (not a view.html.php file). Maybe that is causing your problem since you created a wrong file for this ajax call.



                    And if that is just a mistake in your question, then you also might want to study this earlier answer about ajax calls within Joomla here (it is better not to repeat that here):
                    What is the proper way to make an AJAX call in component?






                    share|improve this answer






















                    • Woops, my fault... It is a view.ajax.php I've created. Thanks for your suggestion!
                      – JonasB
                      29 mins ago










                    • I added a link in my answer about the proper ajax calls in Joomla, if you did not study that before.
                      – Zollie
                      17 mins ago












                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Maybe I misunderstand you in your question but in the directory of com_content/views/article you cannot/should not create a view.html.php file since it originally has a good view.html.php file.



                    In the tutorial which you follow the author says that you should create an 'view.ajax.php' file in com_content/views/article folder (not a view.html.php file). Maybe that is causing your problem since you created a wrong file for this ajax call.



                    And if that is just a mistake in your question, then you also might want to study this earlier answer about ajax calls within Joomla here (it is better not to repeat that here):
                    What is the proper way to make an AJAX call in component?






                    share|improve this answer














                    Maybe I misunderstand you in your question but in the directory of com_content/views/article you cannot/should not create a view.html.php file since it originally has a good view.html.php file.



                    In the tutorial which you follow the author says that you should create an 'view.ajax.php' file in com_content/views/article folder (not a view.html.php file). Maybe that is causing your problem since you created a wrong file for this ajax call.



                    And if that is just a mistake in your question, then you also might want to study this earlier answer about ajax calls within Joomla here (it is better not to repeat that here):
                    What is the proper way to make an AJAX call in component?







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 20 mins ago

























                    answered 48 mins ago









                    Zollie

                    565




                    565











                    • Woops, my fault... It is a view.ajax.php I've created. Thanks for your suggestion!
                      – JonasB
                      29 mins ago










                    • I added a link in my answer about the proper ajax calls in Joomla, if you did not study that before.
                      – Zollie
                      17 mins ago
















                    • Woops, my fault... It is a view.ajax.php I've created. Thanks for your suggestion!
                      – JonasB
                      29 mins ago










                    • I added a link in my answer about the proper ajax calls in Joomla, if you did not study that before.
                      – Zollie
                      17 mins ago















                    Woops, my fault... It is a view.ajax.php I've created. Thanks for your suggestion!
                    – JonasB
                    29 mins ago




                    Woops, my fault... It is a view.ajax.php I've created. Thanks for your suggestion!
                    – JonasB
                    29 mins ago












                    I added a link in my answer about the proper ajax calls in Joomla, if you did not study that before.
                    – Zollie
                    17 mins ago




                    I added a link in my answer about the proper ajax calls in Joomla, if you did not study that before.
                    – Zollie
                    17 mins ago

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fjoomla.stackexchange.com%2fquestions%2f23583%2frun-function-in-com-content-with-ajax-doesnt-call-it-corretly%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery