drmq16019 2016-04-12 13:28
浏览 85

jQuery load()在chrome中不起作用?

OK, I know absolutely nothing about jQuery, ajax etc, I have a jQuery / ajax script that i have modified to do what I need, which is to on load of an image, update mysql database by increasing "views" by 1. This runs perfectly if I am in safari, but for some unknown reason, it doesn't seem to work, or is at least intermittent at best, in chrome. I haven't yet tried other browsers.

here is the jQuery:

$('.view-count').load(function(){
var ClientID=$(this).attr('data-ClientID');
$.ajax({url:"inc/view-count.php?ClientID="+ClientID,cache:false,success:function(result){
    return true;
}});
});

the html / php that includes the class and data-ClientID:

$output .= '<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4"><a href="app.php?id=' . $id  . '"><img class="img-responsive center-block img-border view-count" data-ClientID="' . $id . '" src="images/efits/' . $efit . '" alt="' . $bname . '" /></a></div>';

and finally the view-count.php:

$clientID = (int) $_GET['ClientID'];

try {
$querySql = 'SELECT * FROM clients WHERE id LIKE :clientid';
$countquery = $db->prepare($querySql);
$countParams = array(':clientid'=>$clientID);
$countquery->execute($countParams);
$result = $countquery->fetch(PDO::FETCH_ASSOC);
$views = $result['views'];

} catch (PDOException $e) {
echo 'failed to get client views';
}

try {
$countSql = 'UPDATE clients SET views=:newview WHERE id LIKE :id';
    $count = $db->prepare($countSql);
    $countParams = array(':newview' => $views+1,':id' => $clientID);
    $count->execute($countParams);

} catch (PDOException $e) {
echo "failed to increase view count";
}

Why would I be having problems with chrome but not safari? is it something to do with images being cached? or maybe some of the code is deprecated? as i said, i am a complete novice with jQuery / ajax, so I wouldn't even know where to begin to try and solve the problem! Im surprised i managed to make it work in the first place!

as always, any help is greatly appreciated!

Thanks

Kaylee

  • 写回答

1条回答 默认 最新

  • dongou6632 2016-04-13 10:39
    关注

    ok... I've spent the last few hours doing some research on this, and I found a solution that at least works for me.

    Firstly, I was working on my localhost, and from what I can read, load() doesn't work in chrome when working on localhost, due to the "same origin policy", see this feed for more details jQuery .load() not working in Chrome

    Secondly, I was still experiencing the same problem when I uploaded my files to a server, and from some research I discovered chrome was caching the images that were being loaded, and so didn't run the jquery script. If you add something unique to the end of the src url, it forces chrome to reload the image from the server, not the cache, as it is considered a new image. personally, I created a php variable of $time = date(his); knowing this would be unique second to second, and then added it to the image src like so

    src="image-file-path.jpg?unique=<?php echo $time; ?>"
    

    now each time i send a request it doesnt use the cache to load the image, and so my jquery is executed (yaaay)! Hope this helps someone having the same problem.

    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算