首页 > 解决方案 > if 语句针对本地存储的 JSON 对象中的未定义参数

问题描述

所以这个脚本应该验证在 URL 中作为 GET 方法发送的登录信息,从中创建一个字符串,将其附加到 URL 以从服务器获取 JSON,然后将 JSON 存储在 localStorage 中。这一切都很好。

然后它应该根据某些参数评估用户的状态(登录信息是否正确?如果是,他们是管理员吗?)这就是它崩溃的地方。

一切都显示为“无效”,因为“myObjDeserialized.valid”返回为“UNDEFINED”,我不知道为什么,因为 JSON 显示:[accountid:1234 admin:1 userID:“guard” valid:“真的” ]

这是代码。


<!DOCTYPE html>
<html>
<head>
  <script>

    window.onload = function()
    { 
        ///////////////
        // Part 1.
        ///////////////

        //gets parameters   
        var getParameters = window.location.search.substring(1);

        //appends parameters to  desired URL.
        var fullURL = 'http://hh.offsitevision.com:26000/httpHandHeldService.aspx?' + getParameters;

        alert('fullURL is: ' + fullURL);


        ///////////////
        // Part 2.
        /////////////// 
        function readTextFile(file, callback) 
        {
            var rawFile = new XMLHttpRequest();
            rawFile.overrideMimeType("application/json");
            rawFile.open("GET", file, true);
            rawFile.onreadystatechange = function() {
                if (rawFile.readyState === 4 && rawFile.status == "200") 
                {
                    callback(rawFile.responseText);
                }
            }
            rawFile.send(null);
        }


        // apply the "fullURL" from before to the call to return data. 
        readTextFile(fullURL, function(text){

            var data = JSON.parse(text);
            //console.log(data); 


                    ///////////////
                    // Part 3.
                    /////////////// 
                    //sets data for the object.
                    //var myObj = myJSON;


                    //turns the object into a string.
                    var myObj_serialized = JSON.stringify(data);

                    //Stores the object in localStorage.
                    localStorage.setItem("data", myObj_serialized);



                    ///////////////
                    // Part 4.
                    /////////////// 
                    //Gets the object out of storage (and converts it back form a string to an object). 
                    var myObjDeserialized = JSON.parse(localStorage.getItem("data"));

                    console.log(myObjDeserialized);

                    //Checks data in local object and assess it, then displays appropriate code.    



                    ///////////////
                    // Part 5.
                    /////////////// 
                    var userStatus = "";

                    alert('deserialized!  ' + myObjDeserialized.valid)

                    if (myObjDeserialized.valid == true) 
                    { 
                        alert("User is validated."); 
                        userStatus = "User is Validated. "

                        if (myObjDeserialized.admin == 1) 
                        { 
                                alert("User is an Admin. " ); 
                                userStatus = userStatus + "<br>And the User is an Admin. "
                        } 
                        else{
                                alert("User is not Validated.");
                                userStatus = userStatus + "<br>but they are NOT an admin."
                        }
                    } 
                    else{
                        alert("User is not Validated.");

                        userStatus = "The login information you provided is not correct."
                        }


                    ///////////////
                    // Part 6.
                    /////////////// 
                    //Write the assesment information to <p ID="writeFromLocalStorage"></p>
                    document.getElementById("writeFromLocalStorage").innerHTML += userStatus;


        });







    }

  </script>
</head>
<body>

<h1>hello world.</h1> 
<p ID="writeFromLocalStorage"></p>


</body>
</html>

第 1 部分。获取参数并将它们写入 URL 以获取 json。第 2 部分. 获取外部 JSON 第 3 部分. 将从该 URL 返回的 json 写入 LocalStorage。第 4 部分。将 json 从存储中取出。第 5 部分。评估用户状态。第 6 部分。将用户状态写入标记。

似乎第 4 部分或第 5 部分中的某些内容被破坏了,这导致 if 语句中的变量返回未定义。

我很感激帮助。

标签: javascriptjsonserializationlocal-storageundefined

解决方案


啊啊啊啊。当然,一旦发布它,我就会意识到我做了什么。

if 语句中的“==”应该是“=”

没关系。


推荐阅读