admin管理员组文章数量:1193750
I have the following code:
<script type="text/javascript">
$(document).ready(function() {
$("#Save").click(function() {
$.post("url", {
"data": "data"
}, function(data) {
alert(data);
});
});
});
</script>
I'm testing this script, and one of the tests I'm making is, I just close the asp web development server, and click the button.
IE says "access denied" error, I want to catch any error that occurs here, and display a friendly message to the user in this case.
I trying to use try/catch but didn't work...
Any clue?
I have the following code:
<script type="text/javascript">
$(document).ready(function() {
$("#Save").click(function() {
$.post("url", {
"data": "data"
}, function(data) {
alert(data);
});
});
});
</script>
I'm testing this script, and one of the tests I'm making is, I just close the asp.net web development server, and click the button.
IE says "access denied" error, I want to catch any error that occurs here, and display a friendly message to the user in this case.
I trying to use try/catch but didn't work...
Any clue?
Share Improve this question edited Jan 26, 2009 at 1:29 Bruno asked Jan 26, 2009 at 1:23 BrunoBruno 4,50712 gold badges45 silver badges59 bronze badges4 Answers
Reset to default 14Use the $.ajax()
method instead. It has a hook for errors.
For example:
$.ajax({
url: 'document.xml',
type: 'GET',
dataType: 'xml',
timeout: 1000,
error: function(){
alert('Error loading XML document');
},
success: function(xml){
// do something with xml
}
});
This is a useful snippet for tracking down any jquery ajax errors in conjunction with FireBug.
// Displays any ajax errors in the Firebug console instead of hiding them
$(document).ajaxError(function(){
if (window.console && window.console.error) {
console.error(arguments);
}
});
If you use Ajax with jQuery, you may have noticed that you don't get any error messages when things go wrong. Even if you have major bugs in your callback functions, jQuery just silently fails, sweeping any errors under the rug, and leaving you clueless as to what just happened.
After running this code, you'll start getting error messages in your Firebug console (if anything breaks with your Ajax calls or callbacks). The error messages aren't the greatest, but at least you don't have to stay in the dark any longer.
Credit goes to Jesse Skinner
good, I guess javascript support finally also, like this
try{
main code...
}catch(err){
error ocurred...
}finally{
do this anyway...
}
put a wrapper around all your js code like this:
try
{
... js code
}
catch(err)
{
alert(err.toString());
}
But for ajax calls this propably doesn't work.
本文标签: aspnetCatch exceptions in jQueryStack Overflow
版权声明:本文标题:asp.net - Catch exceptions in jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738492784a2089806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论