日期:2014-05-17  浏览次数:20965 次

急求答案, JS中使用jQuery.parseJSON转换出错
JScript code

 var loginId = getCookie("UserName");
        var flag = "true";
        $.ajax({
            url: "../Handler/GetUserInfo.ashx?sign=loadInfo&loginId=" + loginId,
            type: 'get',
            async: false,
            dataType: "text",
            success: function (value) {
                if (value.toString() == "PleaseLogin") {
                    alert("Please Login Thanks!");
                    flag = "1";
                }else
                {

                    var val = jQuery.parseJSON(value);
                    if (val.Sign.toString() == "0") {   //没有获取到用户信息
                        var loginName = document.getElementById("loginId").value;
                        if (loginName == null || loginName == "") {
                            document.getElementById("loginId").innerText = val.LoginId;
                        } else {
                            document.getElementById("loginId").value = val.LoginId
                        }
                        alert("Not Get User Info!");
                        return false;
                    } else {
                        //获取到用户信息
                        var loginName = document.getElementById("loginId").value;
                        if (loginName == null || loginName == "") {
                            document.getElementById("loginId").innerText = val.LoginId;
                        } else {
                            document.getElementById("loginId").value = val.LoginId
                        }
                        document.getElementById("money").innerText = val.Balance;
                        document.getElementById("address").value = val.Address;
                        document.getElementById("email").value = val.Email;
                        document.getElementById("phone").value = val.Phone;
                        document.getElementById("an1").value = val.Answer1;
                        document.getElementById("an2").value = val.Answer2;
                        //给问题赋值
                        var q1 = val.Question1;
                        var q2 = val.Question2;
                        var qs1 = document.getElementById("qs1");
                        var qs2 = document.getElementById("qs2");
                        for (i = 0; i <= qs1.options.length; i++) {
                            if (qs1.options[i].value == q1) {
                                qs1.options[i].selected = 'selected';
                            }
                            if (qs2.options[i].value == q2) {
                                qs2.options[i].selected = 'selected';
                            }
                        }
                    }
                }
            }
        });



------解决方案--------------------
没看到错误,另外
if (value.toString() == "PleaseLogin") {
alert("Please Login Thanks!");
flag = "1";
}else
{
var val = jQuery.parseJSON(value);
if里面可以toString();猜测一下,你返回的值是单个字符串
而在 else 里面,返回的是序列化了的 json?

jQuery里面 document.getElementById("an1").value = val.Answer1; 为什么不直接用 $("#an1").val();呢?