In this article I will explain
how to show asp.net exception error message in jQuery ajax web method calls using error
function in ASP.NET c#, vb.net . Here I will use ajax method with
jquery to show error message.
Following
Jquery Script we will use:
<script type="text/javascript">
$(function
() {
$('#btnSum').click(function () {
var
val1 = $.trim($('#txtVal1').val());
var
val2 = $.trim($('#txtVal2').val());
$.ajax({
type: "POST",
url: "JqueryAjax.aspx/GetSumofNumbers",
data: "{'val1':'"
+ val1 + "', 'val2':'" + val2 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$('#lblMessage').text(response.d)
},
error: function
(data) {
var
r = jQuery.parseJSON(data.responseText);
var
errorMessage = r.Message;
var
exceptionType = r.ExceptionType;
var
stackTrace = r.StackTrace;
$('#divStatus').html("<b>Error Message: </b>" +
errorMessage + "</br></br>"
+ "<b>ExceptionType: </b>"
+ exceptionType + "</br></br>"
+ "<b>Asp.Net StackTrace:
</b>" + stackTrace)
}
});
return
false;
});
});
</script>
Implementation:
Now design your webpage as given below:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Get Original Asp.net Exception
Message in Ajax Method using Jquery</title>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function
() {
$('#btnSum').click(function () {
var
val1 = $.trim($('#txtVal1').val());
var
val2 = $.trim($('#txtVal2').val());
$.ajax({
type: "POST",
url: "JqueryAjax.aspx/GetSumofNumbers",
data: "{'val1':'"
+ val1 + "', 'val2':'" + val2 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$('#lblMessage').text(response.d)
},
error: function
(data) {
var
r = jQuery.parseJSON(data.responseText);
var
errorMessage = r.Message;
var
exceptionType = r.ExceptionType;
var
stackTrace = r.StackTrace;
$('#divStatus').html("<b>Error Message: </b>" +
errorMessage + "</br></br>"
+ "<b>ExceptionType: </b>"
+ exceptionType + "</br></br>"
+ "<b>Asp.Net StackTrace:
</b>" + stackTrace)
}
});
return
false;
});
});
</script>
</head>
<body>
<form id="form1"
runat="server">
<fieldset style="width:500px">
<legend><strong>Get
Exception message using Ajax Method & jquery in ASP.NET </strong></legend>
<div>
<table>
<tr><td>First Number:</td><td><input type="text" id="txtVal1" /></td></tr>
<tr><td>Second Number:</td><td><input type="text" id="txtVal2"
/> </td></tr>
<tr><td>SUM:</td><td><label id="lblMessage"/></td></tr>
<tr><td></td><td><input type="button"
id="btnSum"
value="Get
Sum" /></td></tr>
</table>
<hr />
<div id="divStatus"></div>
</div></fieldset>
</form>
</body>
</html>
ASP.NET code using C#:
Add following Namespace:
using System.Web.Services;
Now add following Code in Code behind File:
public partial
class JqueryAjax
: System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
}
[WebMethod]
public static int
GetSumofNumbers(int val1, int val2)
{
return
val1 + val2;
}
}
VB.NET code:
Add following Namespaces:
Imports System.Web.Services
Now add following Code in Code behind File:
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
<WebMethod()> _
Public Shared Function GetSumofNumbers(ByVal val1 As Integer, ByVal val2 As Integer) As Integer
Return val1
+ val2
End Function
End Class
0 comments:
Post a Comment