【问题标题】:jQuery ajax not working after second call in Internet Explorer在 Internet Explorer 中第二次调用后,jQuery ajax 不起作用
【发布时间】:2013-04-03 01:19:27
【问题描述】:

我和许多其他人一样遇到了 IE 和缓存问题。我有一个拍卖网站,当用户点击出价时,触发此代码

function bid(id){
var end_dateUP=0;

var tupdate=jq("#tupdate"+id).val();

if (tupdate=="lvl1"){
    end_dateUP=20;  
}else if (tupdate=="lvl2"){
    end_dateUP=15;  
}else if (tupdate=="lvl3"){
    end_dateUP=10;  
}else{
    end_dateUP=0;
}

var url = "http://localhost/bid/comet/update-auction.php"; // the script where you handle the form input.
var user_id=<?php echo json_encode($user_id);?>; //here i'm getting id from SESSION
var user_name=<?php echo json_encode($user_name); ?>; //here i'm getting user name from SESSION
jq.ajax({
    type: "POST",
    async: false,
    url: url,
    data: {"auct_id" : id, "user_id" : user_id, "username" : user_name, "end_date" : end_dateUP}, // serializes the form's elements.
    cache:false,
    success: function(data)
    {
        setTimeout('waitForMsg()',100);
        jq("#tupdate"+id).val("");  
        jq('#bid-container'+id).animate({ backgroundColor: "#659ae0" }, 50);
        jq('#bid-container'+id).animate({ backgroundColor: "#FFF" }, 500);

    },
    error: function(xhr, errorString, exception) {
        alert("xhr.status="+xhr.status+" error="+errorString+" exception=|"+exception+"|");
    }
});
}

在 PHP (update-auction.php) 中,我得到了这个发布的 ajax 数据并更新了我的数据库:

$auction_id=$_POST['auct_id'];
$user_id=$_POST['user_id'];
$usr=$_POST['username'];
$enddate=$_POST['end_date'];


//Database update

此代码在 Firefox 或 Chrome 中运行良好。
所以问题是,当我第一次点击出价时,它可以工作,但是当我转到第二页(代码如下):

function pageClick(page){
var url = "http://localhost/bid/auctions-ajax"; // the script where you handle the form input.  

jq.ajaxSetup({ cache: false }); //this line before $.ajax!!!
    jq.ajax({
           type: "POST",
           url: url,
           data: {"page" : page}, 
           async:false, //to sem dodal za časovni zamik
           cache: false,
           success: function(data)
           {
             jq("#a_loader").hide();  
             jq("#show-category").html(data); // show response from the php script.              
           },
            error: function(xhr, errorString, exception) {
            alert("xhr.status="+xhr.status+" error="+errorString+" exception=|"+exception+"|");
        }
    });

}

(触发 ajax 调用并显示第二页的 onClick)然后这个 function bid(id) 停止工作。我已经搜索了诸如cache:false, 添加new Date().time(); 以在 JSON 中发布和发布数据之类的解决方案,但没有运气。 (此外,当我尝试以 JSON 格式发布数据时,我遇到了一些语法错误:Unexpected token

【问题讨论】:

  • 如何“转到第二页”?您是否正在修改 DOM,并替换具有点击处理程序的元素?然后你需要重新绑定处理程序,或者使用.on()的委托。
  • 我已经更新了帖子并添加了如何进入第二页的代码...
  • 出价在#show-category内吗?
  • 是的。使用 pageClick () 我将页码发布到 auctions-ajax (php 文件),它返回-回显数据库的输出(在这种情况下按页面排序),一个完整的拍卖(完整的意思是输出是用 css 和所以一个..)谢谢..

标签: internet-explorer jquery caching


【解决方案1】:

这解决了我的问题:

$.ajax(url,
{
cache: false,
success: function(){
//do something
}
}
);

“原因是,有些浏览器只要get请求中的请求相同,就会缓存结果。”

例子:

function showBootstrapModalPopup(url, containerId, modalId)
{
        $.ajax(url,
        {
            cache: false,
            success: function (data) {
                //do something
                $('#' + containerId).html(data);

                $('#' + modalId).modal('show');
            }
        }
        );


    //$.ajax({
    //    url: url,
    //    success: function (data) {
    //        $('#' + containerId).html(data);

    //        $('#' + modalId).modal('show');
    //    },
    //    cache: false
    //});
}

【讨论】:

    【解决方案2】:

    如果您当前正在绑定点击处理程序:

    $("selector").click(function...);
    

    改成:

    $("#show-category").on("click", "selector", function ...);
    

    这是必要的,因为pageClick() 替换了之前具有点击绑定的 DOM 元素。您需要使用在整个页面替换过程中保留的父元素的委托。

    【讨论】:

    • 嗯.. 但是我没有你说的“点击”功能-> $("selector").click(function...); 。该页面在拍卖-ajax php 文件中调用如下:&lt;a href='javascript:void()' onClick='pageClick(1)'&gt;&lt;/a&gt; 在此示例中转到第一页。然后这个function pageClick(page) 执行...谢谢你的帮助!
    • 这是进入下一页的代码。您如何绑定功能以点击出价,这也与onclick 绑定?您使用 AJAX 加载的 HTML 中的绑定是否存在?
    • 是的,也在 ajax -> auctions-ajax php 文件中。当我单击 BID 按钮 &lt;a class="bid-button" id="bid-button'.$r['id'].'" href="javascript:void(0)" onClick="bid('.$r['id'].');return false;"&gt;BID&lt;/a&gt; 时,它会触发此 javascript function bid(id)
    • 如果不查看上下文中的所有内容,很难猜测您的应用程序中出了什么问题。您在控制台中看到任何错误吗?当您在检查器中查看 DOM 时,您是否看到了 onclick 属性,它的参数是否正确?
    • onClick 应该是 onclick。会不会是这个问题?
    【解决方案3】:

    好的,我找到了解决方案。 Internet Explorer 的 jquery 动画有问题。我有一些旧的,这导致了问题并且没有完全执行脚本。我必须包含最新的&lt;script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"&gt;&lt;/script&gt; 才能在 IE 中工作。现在效果很好。

    【讨论】:

      猜你喜欢
      • 2012-07-31
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多