【问题标题】:jquery getjson function: Call back returns wrong stringjquery getjson函数:回调返回错误的字符串
【发布时间】:2016-03-15 16:57:19
【问题描述】:

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

            }
        }
    });
});

“倒计时”是一个返回此字符串的 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”返回的确切字符串,它更大并且包含一些空格!我还检查了“auctions.length”的值,它是 7! 我不知道为什么会这样。

当我将 $.get 更改为 $.getJSON 时,也不会显示倒计时。这是为什么呢?

感谢您的关注:) 感谢您对我的英语错误的宽容。

【问题讨论】:

    标签: javascript jquery get getjson countdown


    【解决方案1】:

    因为你的响应数据不是 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");
                    }
    
                }
            }
        });
    });
    

    【讨论】:

    • 在输出 HTML 以外的内容时,不要忘记设置 Content-Type 响应标头。 PHP 默认声明所有输出都是 HTML。
    • @Learning:谢谢,但它不起作用。仍然使用 getJSON 我没有倒计时。有趣的是当我添加 console.log(data); -当我使用 get()- 这是控制台向我显示的内容:609##stop||610##stop||611##2016/03/15 20:20:56||612##2016/04/03 01:00:00|| .....它将整个 html 附加到“数据”!
    猜你喜欢
    • 2011-02-18
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多