【问题标题】:Getting some data from a PHP (json_encode) and now need to access it within the javascript?从 PHP (json_encode) 获取一些数据,现在需要在 javascript 中访问它?
【发布时间】:2011-09-28 20:34:29
【问题描述】:

我目前正在使用 Phonegap 和 XUI 来创建网络应用程序。

我正在通过 XUI 使用 http 请求从外部域检索一些数据。

这工作正常,我正确接收到 JSON 数据,请参阅下面的数据格式:

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

所以现在我希望能够使用 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
    }
    else // Login incorrect
    {alert('Sorry you login details were incorrect please try again.');}
});

我知道这可能很简单,但我似乎无法弄清楚,因此非常感谢任何帮助。

谢谢,

凯恩

【问题讨论】:

    标签: php javascript ajax json xui


    【解决方案1】:

    JSON 对象访问器语法是 object.key,因此如果 this.responseText{"first":"John","last":"Smith","HighScore":"75"},那么您将显示 Smiththis.responseText.last

    警报的示例用法可能是:

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

    【讨论】:

    • 我以前试过这个,因为我以前用过这个方法。我尝试了alert(this.responseText.highscore);,但出现了“未定义”。感谢您的帮助。
    • 区分大小写。应该是 this.responseText.HighScore -- 这是一个演示:jsfiddle.net/mm9rc
    猜你喜欢
    • 2023-03-24
    • 2011-11-05
    • 2014-10-17
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多