【问题标题】:Ajax call only updating onceAjax 调用只更新一次
【发布时间】:2012-02-02 16:37:53
【问题描述】:

我有一个通过 PHP 从 MySQL 数据库填充的数据表。第一列是一个标志功能,因此当单击任何特定行上的图钉图标时,该字段的数据库将更新为“y”或“n”。然后,我可以对该列进行排序,将我所有标记的行带到表的顶部。由于我的表相当大,我决定通过 Ajax 来实现。但是,它仅适用于第一个呼叫,然后变为非活动状态。第一次请求后数据库也不会更新。

这里是 HTML/PHP。

<td align="center" id="pin_record">
<?php // Flag Column    
if ($row['flag'] == 'n'){ ?>        
    <a class="flag_image" href="list_all_quotes.php?id=<?php echo $row['quotes_id'].'_y'; ?>">
    <img src="../images/flag_off.gif" border="0" width="17" height="17" alt="Flag Off">
<?php } elseif ($row['flag'] == 'y'){ ?>
    <a class="flag_image" href="list_all_quotes.php?id=<?php echo $row['quotes_id'].'_n'; ?>">
    <img src="../images/flag_on.gif" border="0" width="17" height="17" alt="Flag On">
<?php } ?>
</a>
</td>

这里是 jQuery

function ajax_init()
{
    $('#pin_record a:first-child').on("click",(flag_record));
}

function flag_record()
{
    var id_pin = this.href.replace(/.*=/,'');
    var id_array = id_pin.split("_"); // split the quote id and pin
    var id = id_array[0];
    var pin = id_array[1];
    this.id = 'flag_link_'+id;
    $.getJSON('deletequote.php?ajax=true&id='+id+'&pin='+pin,set_flag);
    return false;
}

function set_flag(data)
{
    if(!data.success)return alert(data.serverMessage);
    var href = '<a class="flag_image" href="list_all_quotes.php?id='+data.id+'_'+data.pin+'">';
    $('#flag_link_'+data.id)
    .replaceWith(href+data.flagimg);
}

$(document).ready(ajax_init);

我对使用 Ajax 还是很陌生,因此我们将不胜感激。

非常感谢, 克里斯

【问题讨论】:

    标签: php jquery mysql ajax datatables


    【解决方案1】:

    您的请求正在被缓存。使用唯一的 URL 打破缓存:

    'deletequote.php?ajax=true&id='+id+'&pin='+pin,set_flag+"&now="+new Date()
    

    【讨论】:

    • 您好,感谢我尝试并测试过的想法,但这不起作用。反正我已经把它留在原地了。在第一次 ajax 调用之后,再次单击同一链接会执行完整的服务器请求并完全绕过 ajax 代码。
    • 我在这里还缺少其他解决方案吗?
    • 加载 firefox + firebug 并打开 NET 面板。您可以看到您的请求发生了什么。可能是服务器响应问题。
    • 所有事件的服务器响应都是 200 OK。如前所述,第一个 onclick 事件将 Ajax 查询发送到服务器,并得到正确处理。但是在同一行再次按下相同的锚点会完全绕过 jQuery。
    猜你喜欢
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多