从 PHP (json_encode) 获取一些数据,现在需要在 javascript 中访问它?

发布于 2024-12-07 09:48:49 字数 695 浏览 4 评论 0原文

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

我正在通过 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.');}
});

我知道这可能很简单,但我似乎无法弄清楚,所以任何帮助将不胜感激。

谢谢,

凯恩

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
    }
    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流殇 2024-12-14 09:48:49

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!');

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!');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文