English 中文(简体)
使用$. post 验证用户返回自定义错误/成功消息
原标题:Using $.post to authenticate user to return custom error / success message

我要用这个 jQuery 验证用户进入网站, 我如何才能用 login. php 来为成功/ error 返回两个动作? 如果登录信息在登录. php 中不成功, 是否可以刷新成功( 例如) 页面和错误后信息 。 我希望我的问题是明确的。 这是代码。 现在它只是粘贴登录. php 的数据输出, 我不知道如何保存成功/ error 返回服务器的侧面 。

$("#form-login").submit(function(event) {
    event.preventDefault();
    var $form = $( this ),
        log = $form.find(  input[name="log"]  ).val(),
        pwd = $form.find(  input[name="pwd"]  ).val(),
        rememberme = $form.find(  input[name="rememberme"]  ).val();
    $.post(  /ajax/login.php , { log: log, pwd: pwd, rememberme: rememberme },
        function(data) {
            $(".form-login-status").html(data);
        }
    );
});
最佳回答

在您的服务器侧端脚本中, 返回状态和信件, 像这样 :

$result = array();

if (login_is_successful()) {
    $result[ status ] = true;
    $result[ message ] = "Logged in";
} else {
    $result[ status ] = false;
    $result[ message ] =  Incorrect password ;
}

echo json_encode($result);

这将给予您从服务器侧面发送任何回复信息的灵活性, 无需在客户端代码中特别处理。 明天, 如果您想要单独发送不正确的用户名和不正确的密码, 只要 $result[ status] 仍为 false , 您可以在不改变 JS 的情况下发送此信息 。

在您 $. post 的成功处理器中,请这样做:

success : function(resp) {
    if (resp.status) {
        //user logged in
        //refresh page
        window.location.reload();
    } else {
        //failed authentication
        alert(resp.message);
    }
}

确保设置 dataType: json

<强 > 进口说明:

如果认证失败, 您 < code>$. post 中的erorr 处理器将不会被调用, 因此您必须在成功处理器中检查失败的认证 。

只有在 POST 请求发生错误时才呼叫错误处理器 。

问题回答

当然,它出现在文件中:

$.post("example.php", {},  function() {
      alert("success");
    })
    .success(function() { alert("second success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

在您的成功处理器中, 您需要处理被请求的 URI 所返回的旗帜, 该标记显示证书是否验证了用户。 您会想要根据这个逻辑来解释自己的逻辑 。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.