Javascript ajax在调用过于频繁时提供相同的数据
我有一个条形码枪,当我扫描一个条形码时,它会运行下面的功能,但当我扫描5-6个条形码太快时,它会给出重复的数据 我还需要async为真,否则它会很慢 有没有办法解决这个问题,这样我就不会有重复的数据Javascript ajax在调用过于频繁时提供相同的数据,javascript,ajax,loops,exit,Javascript,Ajax,Loops,Exit,我有一个条形码枪,当我扫描一个条形码时,它会运行下面的功能,但当我扫描5-6个条形码太快时,它会给出重复的数据 我还需要async为真,否则它会很慢 有没有办法解决这个问题,这样我就不会有重复的数据 function getUnReadBox() { $("#unReadBoxList").children().remove('li') ; $.ajax({ dataType: "json", url: xxxx.php , s
function getUnReadBox() {
$("#unReadBoxList").children().remove('li') ;
$.ajax({
dataType: "json",
url: xxxx.php ,
success: saveUnRead ,
error: function ( xhr , b , c ) {
$("#reportMsg").html ( "error" ) ; },
async: true });
}
function saveUnRead ( json ) {
var i ;
var new_item ;
var msg ;
for ( i in json ) {
new_item = '<li>' + json[i].PACKAGE_ID + "</li>" ;
$("#unReadBoxList").append ( new_item ) ;
scShipping.unReadBox ++ ;
$("#unReadBox").html ( msg ) ;
}
$("#unReadBoxList").listview('refresh') ;
}
但是我仍然得到重复的数据我和你以前有同样的问题。我得到的解决方案是在调用php程序时传入一个数字时间戳作为参数,或者传入一个日期作为参数。比如说: xxxx.php?日期=2014-03-28 20:54:52:03
它对我有用,我希望它对你也有用…发生这种情况可能是因为web浏览器缓存了你请求的数据,通过避免这种情况,你可以尝试以下方法:
POST
而不是GET
。在$.ajax
方法的参数中将POST
分配给type
$.ajax
方法的参数中将false
分配给cache
xxxx.php?t=Math.random()
或xxxx.php?t=new Date()
。这样可以确保您的请求URL不同,并且不会缓存响应数据就像@user3311636建议的那样,您可以在Ajax调用代码中添加一个额外的参数。因此服务器或浏览器不会缓存响应 像这样试试(答案是@user3311636,为了清晰起见,我只包含了整个函数) 尝试将
new Date().getTime()
附加到URL。
$("#unReadBoxList").children().remove('li') ;
var d = new Date();
var num = d.getTime();
var mySQL = scShipping.jsonUrl+'scTripUnRead.php?T='+scShipping.tripId+"&O="+scShipping.whichOp+"?date="+num ;
$.ajax({
dataType: "json",
url: mySQL ,
success: saveUnRead ,
error: function ( xhr , b , c ) {
$("#reportMsg").html ( "error" ) ; },
async: true });
}
$.ajax({
dataType: "json",
url: "xxxx.php?date=2014-03-28 20:54:52:03" ,
success: saveUnRead ,
error: function ( xhr , b , c ) {
$("#reportMsg").html ( "error" ) ; },
async: true });