首页 新闻 赞助 找找看

jquery异步回调判断ture false

0
悬赏园豆:10 [已解决问题] 解决于 2012-08-21 09:33

前台JS

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>New Document </title>
<script type="text/javascript" src="js/jquery-1.4.1.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>

<script type="text/javascript">
 function aj(){
 alert("fsdf");
 var openId=null;//'0E0C36C83EA68C8058F9ACE3CBFDDCEE';
 $.post(
                "Handle.aspx",
                { openId:openId},
                function (data){
                alert("返回"+data);
               if(data){
                 alert("0返回"+data);
                   window.location.href="http://cloud.suridea.cn/Index.aspx";
             
               }else{
                alert("1返回aa"+data);
                window.location.href="http://cloud.suridea.cn/user/register.aspx?openid="+openId;
               }
            });

   }           
</script>

 
</head>
<body>
<span id="wb_connect_btn"></span>
    <input id="Button1" onclick="aj();" type="button" value="button" />

</body>
</html>

 

后台cs

 protected void Page_Load(object sender, EventArgs e)
        {


            if (HttpContext.Current.Request["openId"] != null)
            {

                Suridea.CloudService.Model.User user = new Suridea.CloudService.Model.User();
                user.Remark = Request["openId"].ToString();
                IList<User> u = userService.FindUsers(user);
                bool result=false;
                if (u.Count != 0)
                {
                    result = true;

                }
                else {
                    result = false;
                }
                HttpContext.Current.Response.ContentType = "text/plain";
                HttpContext.Current.Response.CacheControl = "no-cache";
                HttpContext.Current.Response.Write(result);
                HttpContext.Current.Response.Flush();

            
            }
        }

以上为我js代码 很头疼啊 处理代码

我代码异步回调成功了 返回data有时候明明是false却还是跑到if里面,然后我把data改为返回字符串还是if条件那判断感觉没起到效果。很头疼很急的问题 搞了一天了 都没弄出来哪的问题,希望帮忙快点解决,在线等。

lin_yue的主页 lin_yue | 初学一级 | 园豆:151
提问于:2012-08-20 11:24
< >
分享
最佳答案
0

直接返回链接不就行了么。

收获园豆:10
Vincent8464 | 初学一级 |园豆:17 | 2012-08-20 11:34

怎么返回 我对js不是很懂 请指点下 谢谢你!

lin_yue | 园豆:151 (初学一级) | 2012-08-20 11:35

@lin_yue: 你现在result返回的不是bool么,你换成你要跳转的链接,前端js直接接收值跳转。

Vincent8464 | 园豆:17 (初学一级) | 2012-08-20 11:42

@__s: 参考了你的 回答可以了 但是我很想不通为什么我那data=false怎不不走else很纳闷啊。唉,

lin_yue | 园豆:151 (初学一级) | 2012-08-21 09:33
其他回答(6)
0
 if(data == "True"){

试试这样判断

artwl | 园豆:16736 (专家六级) | 2012-08-20 11:31

试过了 没用 唉 试过了 很多种啊

支持(0) 反对(0) lin_yue | 园豆:151 (初学一级) | 2012-08-20 11:34

@lin_yue: 你的Handle.aspx前台代码是什么样的?

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2012-08-20 11:41
0

先Response.Clear();最后要Response.End();

后台输出的是字符串,不能用if(data){...。

这样试下:

HttpContext.Current.Response.Clear();
string result="";
                if (u.Count != 0)
                {
                    result = "不为空的值就行";
                }
                HttpContext.Current.Response.ContentType = "text/plain";
                HttpContext.Current.Response.CacheControl = "no-cache";
                HttpContext.Current.Response.Write(result);
                HttpContext.Current.Response.Flush();      
                HttpContext.Current.Response.End();
向往-SONG | 园豆:4853 (老鸟四级) | 2012-08-20 12:17
0

贴主看了你的代码,你将数据提交到了一个aspx文件来处理,为了以绝后患!!!

 

我教你一个方法。

 

新建一个"一般处理程序”。后缀是ashx文件。

 

写入以下代码:

 

        public void ProcessRequest(HttpContext context)
        {

            if (context.Request["openId"] != null)
            {

                if (context.Request["openId"] != null)
                {

                    Suridea.CloudService.Model.User user = new Suridea.CloudService.Model.User();
                    user.Remark = context.Request["openId"].ToString();
                    IList<User> u = userService.FindUsers(user);
                    bool result = false;
                    if (u.Count != 0)
                    {
                        result = true;

                    }
                    else
                    {
                        result = false;
                    }
                    context.Response.ContentType = "text/plain";
                    context.Response.CacheControl = "no-cache";
                    context.Response.Write(result);
                    context.Response.Flush();


                }




            }
        }
需要格局 | 园豆:2145 (老鸟四级) | 2012-08-20 14:44
0

if(data.result)

紫·藤萝 | 园豆:202 (菜鸟二级) | 2012-08-20 15:54
0

就事论事

alert("返回"+data);

这里弹出什么内容?

八戒的师傅 | 园豆:1472 (小虾三级) | 2012-08-20 22:17

data=false 谢谢大家对我的指点,你们说的我都试过了,还是让它直接返回连接路径算啦,不过if那里判断不起作用,很纳闷。不知道哪的问题。

支持(0) 反对(0) lin_yue | 园豆:151 (初学一级) | 2012-08-21 09:35

@lin_yue: 

后台输出返回到data里的是字符串"false"而不是bool值,任何不为空的字符串都为真,当然进if里面了,详见我上面的回复。

支持(0) 反对(0) 向往-SONG | 园豆:4853 (老鸟四级) | 2012-08-21 21:33
0

data里面是字符串,并且是"True"不是“true”,是“False”不是"false"

Li,Kan | 园豆:202 (菜鸟二级) | 2013-05-04 15:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册