jquery getjson 函数:回调返回错误的字符串
jquery getjson function: Call back returns wrong string
我正在尝试使用jQuery插件进行倒计时。(我也使用 php 框架)。这是我在主页上的脚本:
<script type="text/javascript">
$(document).ready(function () {
$.get(baseUrl + "/countdown", {id: "609|610|611|612"}, function (data) {
auctions = data.split("||");
for (n = 0; n < auctions.length; n++) {
if (auctions[n] != undefined) {
divis = auctions[n].split("##");
if (divis[1] != "stop") {
$('#bid' + divis[0]).countdown(divis[1], function (event) {
var totalHours = event.offset.totalDays * 24 + event.offset.hours;
$(this).html(event.strftime(totalHours + ' hr %M min %S sec'));
});
} else {
$('#bid' + divis[0]).html("closed");
}
}
}
});
});
"Countdown"是一个PHP文件,它返回以下字符串:
609##stop||610##stop||611##2016/03/28 13:00:56||612##2016/04/03 01:00:00||
使用它会导致我太多错误。 如果我将"auctions.length"更改为"4",一切都会变得正常! 我检查了"data"的值,而不是"countdown.php"返回的确切字符串,它更大并且包含一些空格!我还检查了"拍卖长度"值,它是 7!我不知道为什么会这样。
此外,当我将 $.get 到 $.getJSON 时,没有显示倒计时。 这是为什么呢?
感谢您的关注:)并感谢您对我的英语错误的容忍。
因为你的响应数据不是JSON格式的,所以$.getJSON什么都没有,我建议你使用JSON格式进行响应。
在 PHP 中
$data = array(
array(
'id' => '609',
'datetime' => 'stop'
),
array(
'id' => '610',
'datetime' => 'stop'
),
array(
'id' => '611',
'datetime' => '2016/03/28 13:00:56'
),
array(
'id' => '612',
'datetime' => '2016/04/03 01:00:00'
)
);
echo(json_encode($data));
和在JavaScript中
$(document).ready(function () {
$.getJSON(baseUrl + "/countdown", {id: "609|610|611|612"}, function (auctions) {
for (n = 0; n < auctions.length; n++) {
if (auctions[n] != undefined) {
divis = auctions[n];
if (divis.datetime != "stop") {
$('#bid' + divis.id).countdown(divis.datetime, function (event) {
var totalHours = event.offset.totalDays * 24 + event.offset.hours;
$(this).html(event.strftime(totalHours + ' hr %M min %S sec'));
});
} else {
$('#bid' + divis.id).html("closed");
}
}
}
});
});
相关文章:
- 将字符串拆分为数组时出现JavaScript错误
- gulp Iconify任务抛出错误TypeError:Path必须是字符串.收到false
- 内联javascript与"<脚本>"字符串错误地关闭了脚本标记
- 如何在不创建格式错误的HTML标记的情况下分解字符串
- 如何使用promise正确处理express.js中的错误(字符串或对象)
- 未捕获的类型错误:字符串不是函数
- 错误=字符串.$variable - 失败
- 未捕获的引用错误: 字符串未定义客户端模板.js?rev=J%2BGzwlHEWSv11%2BqD9XeOaw%3D%3
- 未捕获的类型错误:字符串不是函数
- 可以通过主干集合查看错误字符串不是函数
- 使用正则表达式替换XML属性内错误字符串中的嵌套引号
- 抛出新的错误(字符串)不显示在F12
- Json数组中返回错误字符串
- Jquery:使用字符串作为函数(错误:字符串不是函数)
- 如何从没有第一个元素的json对象返回错误字符串
- 从jQuery Ajax调用中获取错误字符串
- 未捕获的类型错误:字符串不是函数,并且没有同名的函数
- 错误:字符串文字未终止
- Chrome在JS控制台上给了我一个错误:“未捕获的无效字符错误:字符串包含无效字符
- javascript语法错误:字符串文字未终止