jQuey ajax 笔记

浏览器端

<script type="text/javascript">

$('#submit').click(function(){
$email  = $('#retake_psd_email').val();
$url    = "http://jsb.php-php.com/user/get_user_id?email="+$email+"&jsoncallback=?";
$.getJSON( $url, function(data){
    if ( data.error == 1 )
        alert_error( 'email 格式错误');
    else if ( data.error == 2 )
        alert_error( '用户不存在');
    else if ( data.error == 0)
    {

        $('#value').text( data.user );
    }

});

});

 服务器端

<?php

$callback    = $_GET['jsoncallback'];
$email         = $_GET['email'];

echo "$callback({error:0,user:$email})";

var $button_html    ='<input type="button" ';
var $button_html2   = ' value="下一步" />';

$(function(){

    $('#retake_psd').click(function() {
        var username = $('#retake_psd_email').val();
        var coolgw_captcha = $('#captcha').val();
        if(username == "" || !username.match(_reg_email_)) {
            alert_error("E-mail格式错误");
            return;
        }
        if ( coolgw_captcha.length != 7 ) {
            alert_error( "验证码错误" );
            return;
        }
        $.ajax({
            type:'POST',
            url:base_url+'retakepajax',
            data:"email="+username + "&captcha="+coolgw_captcha,
            success:function(msg) {
                msg     = eval("("+msg+")");
                if ( msg.num == 0 )
                    alertor( 'E-mail格式错误' );
                else if ( msg.num == 1 )
                    alertor( '用户不存在' );
                else if ( msg.num == 2 )
                    alertor( '用户未设置密保' );
                else if ( msg.num == 4 )
                    alertor( '验证码错误' );
                else if ( msg.num == 3 )
                {
                    var $html   = '';
                    $(msg.msg).each(function(i,msg){
                        $html   += "<h3>" + msg.question + "</h3>"
                                +  '<input type="text" style="height:28px; width:263px;" class="user_answer"><br />'
                                +' <input type="hidden" class="user_question_id" value="'+msg.id+'"><br />';
                    });

                    $html   += '<input type="hidden" id="user_id" value='+msg.email+'><br />';
                    $html   += $button_html + ' onClick="javascript:submit_answer()"' + $button_html2;

                    $("#before_send").html($html);
                }
            }
        });

    });
});
function submit_answer()
{
    var $user_question_id   = $('.user_question_id');
    var $user_answer        = $('.user_answer');
    var question_id         = {};
    var $data               = '';
    var flug    = 1;
    $user_question_id.each(function(i,x){
        question_id[i] = $(x).val();
    });
    $user_answer.each(function(i,x){
        if ( $(x).val() == "" ) {
            alertor("请输入问题答案");
            flug    = 0;
        }
        $data +=  'user_answer['+question_id[i]+']=' + $(x).val()+'&';
    });
    if ( flug == 0 )
        return;
    $data   += 'email=' + $("#user_id").val();
        $.ajax({
            type:"POST",
            url:base_url+"retakswer",
            data:$data,
            success:function(msg){
                data    = eval("(" +msg+ ")");
                if ( data.num == 0 )
                    alertor( 'E-mail 错误' );
                else if ( data.num == 1 )
                    alertor( '回答错误' );
                else if ( data.num == 2 ) {
                    var $html   = '<h3>新的密码</h3><input type="password"  style="height:28px; width:263px;"  class="user_psd"><br/>'
                                +  "<h3>确认新的密码</h3>" + '<input type="password"  style="height:28px; width:263px;"  class="user_psd"><br />';

                    $html   += '<input type="hidden" id="retakepsd_rand_str" value='+data.str+'><br />';
                    $html   += $button_html + ' onClick="javascript:submit_psd()" ' + $button_html2;

                    $("#before_send").html($html);

                }
            }
        });
}

function subpsd()
{
    var $data   = '';
    $('.user_psd').each(function(i,x){
        $data   += "password[]="+$(x).val()+"&";
    });

    $data   += "rand_str=" + $("#retakepsd_rand_str").val();
    $.ajax({
        type:"POST",
        url:base_url+"retakge_psd",
        data:$data,
        success:function(msg) {
            data    = eval("("+msg+")");
            if ( data.num == 0 ) {
                alertor('请重新输入');
                setTimeout(function(){window.location.href=window.location.href},100);
            } else if ( data.num == 1 )
                alertor( data.msg );
            else if ( data.num == 2 ) {
                alertor('修改成功');
                setTimeout(function(){window.location.href=base_url},100);
            }
        }
    });
}

$.post(base_url+"login",{email:tmpusername,password:tmppwd},function(data) {
            var objJson = eval("("+data+")");
            if(objJson['code'] == 1) {
                alert("error!");
                return;
            }
            if(objJson['code'] == 0) {
                alert("bugs!");
                return;
            }
            if(objJson['code'] == 4) {
                location.href = base_url;
                return;
            }

        });