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)
parse JSON from PHP file to javascript
-
22-07-2023 - |
Domanda
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');
}
});
}
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow