0

在用户对服务器的登录请求成功后,我正在努力重定向用户。服务器的响应是成功的,我能够在警报框中显示值,但我没有被重定向到不同的页面。

这是我正在使用的代码

<script>

    require(["dojo/dom", "dojo/on", "dojo/request", "dojo/dom-form", "dojo/domReady!", "dojo/json"],

        function(dom, on, request, domForm, JSON, cookie){

            var resultDiv = dom.byId("resultDiv");

            on(dom.byId("signinButton"), "click", function(evt){


                var emailAddressVar = document.getElementById('emailInput');
                var passwordVar = document.getElementById('passwordInput');

                if(emailAddressVar != "" && passwordVar != ""){
                    request.post("http://xxx.xxx.xx.xx:8080/com.thinkingpa.restful.web/SecurityResourceService/loginUser",{
                        data: {
                            emailAddress:emailAddressVar.value,
                            password:passwordVar.value                     
                        }
                    }).then(function(response){

                        var jsonResponse = "(" + response + ")";
                        var jsonObject = eval(jsonResponse);

                        for(var value in jsonObject){
                            alert(jsonObject[value]);
                        }

                        alert( "Your username is: " + jsonObject.username);

                        if(jsonObject.successful == "true"){
                            window.location = "http://www.mysite.com";
                        }else if(jsonObject.successful == "false"){
                            resultDiv.innerHTML = "<div class=\"info-box\"> <p> The Email or Password you entered was incorrect. </p><p> <a href=\"sign-up.html\">Register</a> to get an account. </p> <div>";
                        }

                        resultDiv.innerHTML = "<div class=\"error\">" + response + "<div>";
                    });
                }else{
                    resultDiv.innerHTML = "<div class=\"info-box\"> <p> Please Fill Out All The Required Fields </p> <div>";
                } 
            });
        }
    );

</script>
4

1 回答 1

0

True & false 不是字符串。试试这个:

if(jsonObject.successful == true)
    window.location = "http://www.mysite.com";
else
    resultDiv.innerHTML = "<div class=\"info-box\"> <p> The Email or Password you entered was incorrect. </p><p> <a href=\"sign-up.html\">Register</a> to get an account. </p> <div>";

做不到,做alert(jsonObject.successful + ' = ' + typeof(jsonObject.successful))

于 2012-10-05T07:33:20.010 回答