开发者

Getting some data from a PHP (json_encode) and now need to access it within the javascript?

开发者 https://www.devze.com 2023-04-09 14:52 出处:网络
I am currently using Phonegap and XUI to create web app. I am retrieving some data from an external domain using a http request via XUI.

I am currently using Phonegap and XUI to create web app.

I am retrieving some data from an external domain using a http request via XUI.

This is working correctly and I receive the JSON data back correctly, see the data format below:

({"first":"John","last":"Smith","HighScore":"75"})

So now I want to be able to access the individual assets of the data using javascript.

 x$('#test').xhr(URL,function() {
    loggedin = this.responseText; // This is the data that has been received from the PHP file
    if(loggedin != '1') // If not 1 then will let them in
    {
        alert(loggedin); // Alerts with the data recieved
   开发者_StackOverflow社区 }
    else // Login incorrect
    {alert('Sorry you login details were incorrect please try again.');}
});

I know its probably simple to do but I just can't seem to figure it out so any help would be much appreciated.

Thanks,

Kane


JSON object accessor syntax is object.key, so if this.responseText is {"first":"John","last":"Smith","HighScore":"75"} then you'd display Smith with this.responseText.last

An example usage for your alert could be:

alert('Hello ' + this.responseText.first + ' ' this.responseText.last + '! You currently have a high score of ' + this.responseText.HighScore + ' points! Play again!');
0

精彩评论

暂无评论...
验证码 换一张
取 消