使用原型版本 1.6.0.2。
我有一个常见的问题,异常在回调函数中抛出时被吞没,通常是当我尝试处理对Ajax.Request
称呼。这是一个简单的例子:
HTML 标记:
<input type="button" id="myButton" value="Press Me" />
JavaScript:
MYSITE = {};
document.observe("dom:loaded", function () {
// Set up our helper object
MYSITE.pageHelper = new MYSITE.PageHelper();
});
MYSITE.PageHelper = function() {
console.log("PageHelper called.");
$("myButton").observe("click", this.makeCall.bindAsEventListener(this));
};
MYSITE.PageHelper.prototype.makeCall = function() {
console.log("Make call.");
new Ajax.Request(
"remoteCall.cfm",
{
method: 'get',
parameters: "",
onComplete: this.handleCallback.bindAsEventListener(this)
});
};
MYSITE.PageHelper.prototype.handleCallback = function(resp) {
console.log("Start callback processing...");
var x = missingVar + "text"; // This line generates an exception...
console.log("Finished callback processing.");
};
好的,所以问题是,如果您在 Firefox 中使用 Firebug 运行此代码,则有问题的行不会输出任何异常 - 它会被吞掉。咕噜咕噜。
我知道捕获这些的唯一方法(假设我正在调试)是将回调函数的内容包装在 try/catch 中。例如:
MYSITE.PageHelper.prototype.handleCallback = function(resp) {
try {
console.log("Start callback processing...");
var x = missingVar + "text"; // This line generates an exception...
console.log("Finished callback processing.");
} catch (e) {
console.log(e);
}
};
还有其他人遇到过这个问题吗?有什么解决方法吗?
提前致谢!
截至今天,这是已知的行为:
这里有一张增强处理这些吞没异常的票证:
https://prototype.lighthouseapp.com/projects/8886/tickets/634-no-exception-on-error-in-oncreate-method-of-ajaxrequest
建议的一种解决方法是添加以下代码(感谢 Glenn Maynard!):
Ajax.Responders.register({
onException: function(request, exception) {
(function() { throw exception; }).defer();
}
});
希望能帮助其他人解决同样的问题,直到实施更永久的解决方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)