jquery getjson 函数:回调返回错误的字符串

jquery getjson function: Call back returns wrong string

本文关键字:错误 字符串 返回 回调 getjson 函数 jquery      更新时间:2023-09-26

我正在尝试使用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");
                }
            }
        }
    });
});