문제

I want to parse json data from php file to javascript file the json object data is parsed and tested but when i want to access to specific element in the object, I faced problem undefined in the alert.

PHP file contains

[{"userID":"2","username":"abdo","password":"abdo","flag":"1","status":"1","typeID":"2"}]

the code:

                login: function() {
                var usr = this.get("username");
                var pass = this.get("password");
                $.ajax({
                    type: "GET",
                    url: "login.php",
                    data: {username: usr, password: pass},
                    dataType: "jsonp",
                    success: function(data) {

                        alert(data[0].username);
                    },
                    error: function() {
                        alert('fail');
                    }
                });
            }
도움이 되었습니까?

해결책

You have an [array] at the top level.
And data is a JSON object already, no need to parse it again. It means you have to do : alert(data[0].username)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top