【问题标题】:return a variable from ajax and php on success成功时从 ajax 和 php 返回一个变量
【发布时间】:2018-05-18 12:11:38
【问题描述】:

我正在使用AJAX (jQuery)user-ajax.php 发布表单,但该页面实际上需要回显一个响应,例如"success"。在 ajax 代码中我测试响应值,如果它等于成功我正在做某事但它在这里不起作用是 php 代码

function ajaxlogin($config,$lang){
   //something
        $response = 'success';
        echo $response;

}

ajax 代码:

$(document).ready(function () {
    $("#login").click(function () {
        $("#login-status").show();
        var action = $("#lg-form").attr('action');
        var form_data = {
            action: action,
            username: $("#username").val(),
            password: $("#password").val(),
            is_ajax: 1
        };

        $.ajax({
            type: "POST",
            url: ajaxurl,
            data: form_data,
            success: function(response) {
                alert (response); //this alert show "success"
                if (response == 'success') {
                    $("#lg-form").slideUp('slow', function () {
                        $("#login-status").removeClass('info-notice').addClass('success-notice');
                        $("#login-status #login-status-message").html('Logged in success. Redirecting....');
                        location.reload();
                    });
                }
                else {
                    $("#login-status").removeClass('info-notice').addClass('error-notice');
                    $("#login-status #login-status-message").html(response);
                }
            }
        });
        return false;
    });
});

我的问题是警报显示"success""if (response == 'success')" 指令未执行它直接转到其他。通常'success'== 'success'

【问题讨论】:

  • 您确定响应字符串不包含任何其他字符吗?比如,你能console.log(response.length) 确保它是 7 吗?
  • 像这里一样修剪它:if($.trim(response)=='success'){} 你可能有一些空白。
  • 您应该添加您正在使用的 PHP 文件/函数function ajaxlogin($config,$lang)的完整且准确的代码
  • 嗨,非常感谢您的帮助,我做了 console.log(response.length) 它显示 8 我使用了 if($.trim(response)=='success') 并且它的工作.非常感谢您的支持

标签: javascript php jquery ajax


【解决方案1】:

非常感谢您的帮助。我完全按照您的建议执行 console.log(response.length) 并且它显示 8。我使用了 if($.trim(response)=='success') 并且它正在工作。非常感谢您的支持

【讨论】:

    猜你喜欢
    • 2015-06-14
    • 1970-01-01
    • 2016-04-06
    • 2015-01-16
    • 2019-05-16
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    相关资源
    最近更新 更多