【发布时间】:2012-08-23 16:56:15
【问题描述】:
我正在使用 jquery.mobile-1.1.1.min.js 和 cordova-2.0.0.js (phonegap)。
在我的 $.ajax 调用设置中,我将参数设置如下:
success: onFetchOK, // saves everything to the database
complete: function(jqXHR, textStatus) {
// show everything in the database
showAllHeaders();
},
error: function(xhr, textStatus, errorThrown) {
alert("onFetchFailure: There was an error retrieving messages: \n"
+"xhr: " + xhr.statusText
+"\nstatus: " +textStatus
+"\nerror: " +errorThrown.message);
$('#fetch-result').html(textStatus);
},
函数 onFetchOK 从服务器获取 18 个消息头,每个消息头都有一个主题和一个日期/时间,这些都使用 phonegap webdb 存储 API 存储在 webdb 中。
如果我删除并重新创建消息表并重新运行应用程序,那么对于我收到的 18 条消息中的每条消息都会调用一次 complete: 函数,因此会显示 342 个消息标头 (18x18+18)。
在我的代码中除了这个之外没有其他地方完整:showAllHeaders();被调用。
我在 showAllHeaders() 中放置了一个警报,该警报被触发了 18 次。
为了进一步缩小这个范围,我在我的成功中放置了一个循环计数器:onFetchOK(),如下所示:
var loopCount = 0;
// process each message
$.each(json.messages, function(index, msg)
{
// save each alert before displaying it
webdb.addAlert(msg.msg_id, msg.title, msg.when);
loopCount++;
});
alert("loopCount = " +loopCount);
当这个警报被调用时,它会显示“loopCount = 18”并且这只会被调用一次。
如果我退出应用程序,重新加载它并且没有从服务器拉取新消息,那么会显示 18 个消息头,这正是它应该的样子。
所以,我的 complete: function() 似乎被重复调用了......一次应该被调用,然后又被调用了 18 次。
有人知道为什么会这样吗?
【问题讨论】:
-
发布的信息不够。请发布整个 AJAX 脚本和启动 AJAX 调用的代码。
-
@ppetree,您是否尝试按照我的建议进行调试?如果是,您能发布更多信息吗?
标签: jquery jquery-mobile cordova-2.0.0