Cia*_*her 5 javascript ajax dhtml prototypejs
使用Prototype版本1.6.0.2.
我有一个常见的问题,当它们被抛入回调函数时会被吞下异常,通常是在我尝试处理对Ajax.Request调用的响应时.这是一个简单的例子:
HTML标记:
<input type="button" id="myButton" value="Press Me" />
Run Code Online (Sandbox Code Playgroud)
使用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.");
};
Run Code Online (Sandbox Code Playgroud)
好的,所以问题是,如果你在Firebug的Firefox中运行这个代码,那么对于有问题的行也不会输出异常 - 它被吞没了.咕嘟咕嘟.我知道捕获这些的唯一方法(比如说我正在调试)是在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);
}
};
Run Code Online (Sandbox Code Playgroud)
还有其他人遇到过这个问题吗?那里有任何解决方法吗?
提前致谢!
截至今天,这是已知的行为:
这里有一张增强处理这些吞没异常的票证:
建议的一种解决方法是添加以下代码(感谢 Glenn Maynard!):
Ajax.Responders.register({
onException: function(request, exception) {
(function() { throw exception; }).defer();
}
});
Run Code Online (Sandbox Code Playgroud)
希望能帮助其他人解决同样的问题,直到实施更永久的解决方案。