【问题标题】:How to authenticate‎ a user in Joomla via AJAX / JQuery如何通过 AJAX / JQuery 在 Joomla 中对用户进行身份验证
【发布时间】:2014-01-16 00:25:46
【问题描述】:

我希望能够通过 AJAX 调用对 Joomla 中的用户进行身份验证,以便在登录不正确时创建错误效果,并在登录正确时重定向用户。

我更愿意通过 JQuery 的 .ajax API 来实现。

另外,我是否需要以某种方式初始化 JQuery,或者它已经存在,你只需要使用“JQuery”而不是“$”?

【问题讨论】:

    标签: javascript jquery ajax authentication joomla


    【解决方案1】:

    试试这个,

    您可以使用 Joomla 的登录选项进行 Ajax 登录验证。

    通过 post 收集您的用户名和密码并设置为数组。

       $options = array();
       $options['remember'] = JRequest::getBool('remember', false);
       $data['username'] = JRequest::getVar('username', '', 'method', 'username');
       $data['password'] = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);
       $credentials = array();
       $credentials['username'] = $data['username'];
       $credentials['password'] = $data['password'];
       $app = JFactory::getApplication();
       $error = $app->login($credentials, $options);
       if (!JError::isError($error)) {
        // login success
        }
      else{
        //Failed attempt
       }
    

    如果你有任何自定义组件,上面的代码部分可以写在你的任何控制器函数中。如果您使用的是Joomla3.x,您可以使用com_ajax 来完成此任务。

     var data = "";//set your user name and password
    jQuery.ajax ({
    
            type: "POST",
    
            url: "index.php?option=com_ajax&task=loginauth",
    
            data: data,
    
            success: function(data) {
             }
              });
    

    当您在应用程序中包含 jQuery 库时,只需使用 jQuery 访问它 '$' 用于 Joomla 中的 moo-tools。对于包含 jQuery 库,您只需编辑模板文件 templates/yourtemplate/index.php

    希望对您有所帮助..

    【讨论】:

    • If your are using Joomla3.x you can use com_ajax for this task. >> 您也可以将 com_ajax 用于 Joomla 1.5 和 2.5,但您需要手动下载(github)并安装它;)
    • 那么,如果我使用的是 3.x,我不需要 PHP 代码吗?只是使用具有用户名和密码的数据对象调用 index.php?option=com_ajax&task=loginauth?
    • @kidalex 无论如何,您必须在控制器上添加 loginauth() 并在其中添加 php 代码。
    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多