在阿贾克斯成功显示用户信息 [英] Display user messages upon ajax success

查看:120
本文介绍了在阿贾克斯成功显示用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示两种在jQuery的/ Ajax响应用户的消息。

I am trying to display two kind of user messages upon jquery/ajax response.

在我处理脚本我保存这些消息是这样的:

In my processing script I store these messages like this:

// Initialize Json array.
$messages ='';

   // Check for the email: 
    if (!empty($_POST['email'])) {  
        $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
        $email = filter_var($email, FILTER_VALIDATE_EMAIL);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            // Not a valid email
            $messages = array('success'=>false,'cssClass'=>'alert-danger','message'=>'Email does not match the required format.');

        }

    // Print a message based upon the result:
    if ($stmt->affected_rows == 1) {                
        // Print a message and wrap up:
        $messages = array('success'=>true,'cssClass'=>'alert-success','message'=>'Your password has been changed. You will receive the new, temporary password at the email address with which you registered. Once you have logged in with this password, you may change it by clicking on the "Password Modification" link.');
    }

    echo json_encode($messages);

jQuery中我试图填充这些消息是这样的。但我不能打印出这些信息。

In Jquery I am trying to populate these message something like this. But I cannot print out those messages.

更新:

success: function (json) {                  
    if (json.success) {
        // it worked
        alert('ok');
        $('#message').append('<div class="alert alert-dismissible ' + json.cssClass + '" role="alert">' + json.message + '</div>');
        $('#message').show();                           

    } else {
        // something went wrong
        alert('not ok');    
        $('#message').append('<div class="alert alert-dismissible ' + json.cssClass + '" role="alert">' + json.message + '</div>');
        $('#message').show();       

    }

谁能告诉我在哪里,我错了? 希望有人可以帮助我。

Can anybody tell me where I am going wrong? Hope somebody may help me out.

感谢您。

推荐答案

您只是缺少一行code。

You are just missing one line of code.

什么是服务器脚本提供的JSON 字符串

What the server script is providing is JSON string.

和你不能从JSON的访问属性的字符串

And you cannot access properties from JSON string.

您必须解析JSON的字符串第一,创建一个JSON的对象出来。

You have to parse the JSON string first and create a JSON object out of it.

success: function (jsonString) {
var json = JSON.parse(jsonString);         //parse the JSON string and create JSON object
if (json.success) {
    // it worked
    alert('ok');
    $('#message').append('<div class="alert alert-dismissible ' + json.cssClass + '" role="alert">' + json.message + '</div>');
    $('#message').show();
} else {
    // something went wrong
    alert('not ok');
    }
}

以上code使用 JSON JS平台提供一流

这篇关于在阿贾克斯成功显示用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆