سؤال

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