dpw70180 2012-02-17 19:38
浏览 36
已采纳

通过JSON,JQuery,PHP传递特定信息

My PHP:

if($count == 1 && $user_level < 4 && $if_active == 1)
{
$response = array("success" => true); 

//The login stuff happens here

echo json_encode($response); 
}

elseif($count == 1 && $user_level < 4 && $if_active == 0)
{
$response = array("success" => not_act);  
echo json_encode($response);  
}
else 
{
$response = array("success" => false); 
echo json_encode($response); 
}

The JQuery that sends and recieves the data from that PHP file

$('#login_form').submit(function(){ 
        var emailB = encodeURIComponent($('#email_B').val());   
        var passwordB = encodeURIComponent($('#password_B').val());   
        $('#wrapper').fadeOut(); 
        $('#login_effect').fadeIn(); 
        $.ajax({ 
            type: 'POST', url: 'checklogin.php',  dataType: "json",  data: { email: emailB, password: passwordB,  }, 
            success: function(result) {                 
                if (!result.success) { timeout = setTimeout(function(){ $('#login_effect').fadeOut(); }, 1500); $('#wrong_password').fadeIn(); }  
                else if (result.success == not_act) {timeout = setTimeout(function(){ $('#login_effect').fadeOut(); }, 1500); $('#not_act').fadeIn();}
                else { timeout = setTimeout(function(){ $('#login_effect').fadeOut(); }, 1500); document.location.href='http://matchmingle.net/profile/'; } 
            }  
        }); 
        return false; 
    }); 

This works fine and dandy EXCEPT for the not activated part. How do I find out in the JQuery what the JSON returned besides just true and false? I tried to

$response = array("success" => not_act);  

and then in the JQUery

else if (result.success == not_act) {do cool stuff}

but I can't get it to work because I'm obviously doing it wrong. BTW I'm very new to this JQuery/JSON/Javascript, what you're seeing me do is what I've learned from a dummies book over the corse of a few weeks and a bunch of help from you guys.

So yeah, what's the proper way to set the json response variable and find out what it's value is in the JQuery? Thanks a bunch -Mike

  • 写回答

3条回答 默认 最新

  • duanpa2143 2012-02-17 19:45
    关注

    I think that you need to add quote mark

    in your php:

    $response = array("success" => "not_act");
    

    and in your javascript:

    else if (result.success == "not_act") {/*do cool stuff*/}
    

    because not_act seem to be a variable name, and I don't see if this variable have the same value in your php code and in your javascript one.

    By surrounding with quote mark, it will now be a string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本