2015-12-18 44 views
0

我正在测试使用AJAX登录到Wordpress。但是我的PHP代码没有将数据返回给我的ajax脚本。为什么我的PHP代码不会将json数据返回给我的Ajax调用?

我想知道我是否错过了一些东西。

我的模板看起来像这样

// This code is at the top of my template 

function authenticate($username, $password) { 
    global $user; 

    $data = array(); 
    $data['user_login'] = $username; 
    $data['user_password'] = $password; 
    $data['remember'] = false; 

    $user = wp_signon($data, false); 

    if (is_wp_error($user)) { 
     $result = array('login' => false, 'error' => $user->get_error_message()); 
    } 
    if (!is_wp_error($user)) { 
     $result = array('login' => true); 
    } 

    echo json_encode($result); 
} 

if (isset($_REQUEST['user_login'])) { 
    authenticate($_REQUEST['user_login'], $_REQUEST['user_password']); 
} 

(...) // My HTML starts here 

<form name="loginform" action="<?= $_SERVER['REQUEST_URI'] ?>" method="post"> 
(...) 
</form> 

这是我的JS代码

$('.login-button').on('click', function(e){ 
    e.preventDefault(); 

    var $form  = $('form'); 
    var form_action = $form.attr('action'); 
    var form_data = { 
     'user_login': $form.find('.user_login').val(), 
     'user_password': $form.find('.user_psw').val(), 
    }; 

    $.ajax({ 
     type: "POST", 
     url: form_action, 
     data: form_data, 
     dataType : 'json', 
     success: function(result){ 
      cl(result); // <-- This is not outputting anything 
     } 
    }); 
}); 

我可能只是缺少的东西在这里根本,但不知道是什么。

+2

'cl(result); // < - 这不是输出任何东西 - - 是否被调用的函数?你为什么没有'错误'功能呢? *在开发者工具控制台上报告* *。开发工具网络标签说什么?你能看到请求吗?它是否得到适当的回应? – Quentin

+0

你应该考虑正确使用AJAX,在* functions.php *中使用'wp_ajax_'动作钩子和回调函数:https://codex.wordpress.org/AJAX_in_Plugins – rnevius

+0

cl()是'console的自定义函数(快捷方式)。日志()'。我正在使用它进行测试。 – Steven

回答

1

你应该添加AJAX回调的functions.php,而不是一个页面模板。

function so_34356392_ajax_auth() { 
    if (isset($_REQUEST['user_login'])) { 
     authenticate($_REQUEST['user_login'], $_REQUEST['user_password']); 
    } 
} 
add_action('wp_ajax_so_34356392_auth', 'so_34356392_ajax_auth'); 
add_action('wp_ajax_nopriv_so_34356392_auth', 'so_34356392_ajax_auth'); 


function authenticate($username, $password) { 
    global $user; 

    $data = array(); 
    $data['user_login'] = $username; 
    $data['user_password'] = $password; 
    $data['remember'] = false; 

    $user = wp_signon($data, false); 

    if (is_wp_error($user)) { 
     $result = array('login' => false, 'error' => $user->get_error_message()); 
    } 
    if (!is_wp_error($user)) { 
     $result = array('login' => true); 
    } 

    echo json_encode($result); 
    wp_die(); // this is required to terminate immediately and return a proper response 
} 

然后你的AJAX功能将变为:

var form_data = { 
    'action': 'so_34356392_auth', // Target the callback in functions.php 
    'user_login': $form.find('.user_login').val(), 
    'user_password': $form.find('.user_psw').val(), 
}; 

$.ajax({ 
    type: "POST", 
    url: form_action, 
    data: form_data, 
    dataType : 'json', 
    success: function(result){ 
     cl(result); 
    }, 
    error: function() { 
     cl('Cannot retrieve data.'); 
    } 
}); 

了解更多关于在WordPress,in the Codex使用AJAX。

+0

伟大的@ mevius,我更喜欢它。但是,Ajax调用的URL是什么? – Steven

+0

@Steven,很好的问题。这真的取决于你的JS在哪里。你是否直接将它包含在你的页面模板中?或者它是一个外部/单独的.js文件? – rnevius

+0

我有单独的JS文件。所以我必须在我的模板的页脚中使用它:''。 – Steven

1
if (isset($_REQUEST['user_login'])) { 
    authenticate($_REQUEST['user_login'], $_REQUEST['user_password']); 
} 

(...) // My HTML starts here 

你输出的JSON,但你也随即输出HTML中。这使得JSON无效。如果你有一个参数,本应该已经被传递给error函数的一个参数发出警告。

只有当您不输出JSON时,才需要输出HTML。

exit()if语句中会做的工作(虽然我重构的东西有两个不同的文件来处理不同类型的输出,并包括基于if不同的...或者再前进一步,并使用完整的MVC框架)。

您在输出JSON时也忘记设置header("Content-Type: application/json");

+0

好的,谢谢。我开始将它放在一个单独的PHP文件中,但之后我无法访问WP函数。 – Steven

-1
if (isset($_REQUEST['user_login'])) { 
    // |-- you need an echo 
    echo authenticate($_REQUEST['user_login'], $_REQUEST['user_password']); 
    exit(); 
} 
+0

“// | - 你需要回声” - 为什么? 'authenticate'函数不返回任何内容。 – Quentin

+0

这段代码(虽然不是echo语句)确实解决了这个问题,但它并没有解释*如何,并且解释是错误的和误导的。 – Quentin

相关问题