php ajax获取循环变量,通过jQuery AJAX设置和获取PHP变量

我试图为自己和朋友制作一个基本的聊天系统。这很简单,然而,ajax让我非常困惑。我使用ajax来请求一个php页面,我设置了一个用户名,以及我最后一次下载聊天。但是,我不知道如何阅读php页面的回声。我已经在网上查了一下,但是有很多变化,让我更加困惑。有人可以给我一些指导吗?

Scripts.js(已经链接了jQuery)

// on document ready + called every second

$.ajax({

url: 'download.php',

type: 'REQUEST',

datatype: 'json',

data: ({

last_downloaded: latestTimestamp,

username: username

}),

success: function (data) {

console.log(data.message[0].time);

// Uncaught TypeError: Cannot read property '0' of undefined

// how do I get the time from the json?

// {"message":{"chat":"Hello, world!","time":"2014-05-09 17:32:00,"username":"Josue"},"message":{"chat":"This is my second message.","time":"2014-05-09 17:32:05,"username":"Josue"}}

}

});

latestTimestamp = data.message[0].time;

downloadingTimer = setTimeout(actuate, timeoutValues[currentTimeoutIndex]);

}

的download.php

这是我的php页面作为普通字符串回显(现在格式正确的JSON):

[

{

"chat":"Hello, world!",

"time":"2014-05-09 17:32:00",

"username":"Josue"

},

{

"chat":"This is my second message.",

"time":"2014-05-09 17:32:05",

"username":"Josue"

}

]

我的页面顶部有header("Content-Type: application/json");。我做错了什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值