0

im using Jquery with Asp.net web service ,i have a form im using jquery ajax functionality for passing data to web service

my problem is when im comment one sqlparameter in webmethod it will give me an exception but this will not call my jquery onError function ,it will always (though the exception or not ) call OnSuccess function

What is the reason for it ?

im using Newtonsoft for serializing response to json format

please Help me this is my client side script

<script type="text/javascript">

$(document).ready(function (){

    var progressOptions={

        steps: 20,
        stepDuration: 20,
        max: 100,
        showText: true,
        textFormat: 'percentage',
        callback: null,
};
 $('#Waitdialog').dialog({

        autoOpen:false,
        width: 300,
        height:150,
        modal: true,
        resizable: false,
        closeOnEscape:false,
        open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
        position:"center",
        draggable:false,
        title:"Done",
        close:false,
        buttons: {
            "Ok": function() {
             $(this).dialog("close");
            }

      }
    });
$("#submit").click(function(){
$("#ProgressBar").progressbar({

        value:50,
        steps: 20,
        step_duration: 20,
        max: 100,
        height: 12,
        showText: true,
        textFormat: 'percentage',
        callback: null,

});

var name = $("#txtName").val();
var userID = $("#txtUserName").val();
var email=$("#txtEmail").val();
var department=$("#cmbDep").val();
var password1=$("#txtPassword").val();
var password2=$("#txtPassword").val();
$.ajax({
    async: false,
    cache: false,
    type: "POST",
    url: "http://localhost:4055/ShareMe/logon.asmx/HelloWorld",
    data: '{"name": "' + name + '", "userID": "' + userID + '","email":"'+email+'","department":"'+department+'","password1":"'+password1+'","password2":"'+password2+'"}',
   // data:"{'funcParam':'"+$('#form1').serialize()+"'}",
   //        data: '{"name": "' + name + '", "userID": "' + userID + '","email":"'+email+'","department":"'+department'"},'
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    error: OnError

});

return false;
function OnSuccess() {

$("#Waitdialog").dialog('open');

  }

  function OnError(xhr, desc, exceptionobj) {
                    alert(xhr.responseText);
                    console.log(xhr.responseText);
                    console.log(desc);
                    console.log(exceptionobj);

                 }
    });

});
</script>

so this is my ASP.net webmethod

<WebMethod()> _
<Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
Public Function HelloWorld(ByVal name As String, ByVal userID As String, ByVal email As String, ByVal department As String, ByVal password1 As String) As String

    Try
        Dim param(6) As SqlParameter
        param(0) = New SqlParameter("@RefId", 1)
        param(1) = New SqlParameter("@Name", name)
        param(2) = New SqlParameter("@UserName", userID)
        param(3) = New SqlParameter("@Email", email)
        param(4) = New SqlParameter("@Department", department)
        param(5) = New SqlParameter("@Password", password1)
        param(6) = New SqlParameter("@CreatedBy", "")

        SqlHelper.ExecuteNonQuery(connStringShareMe, Data.CommandType.StoredProcedure, "Users_Insert", param)

        Return "Done"
    Catch ex As Exception

        Return JavaScriptConvert.SerializeObject(ex.Message)
    End Try

End Function

2 Answers 2

1

this is because of the çatch clause in your web method.
the exception is caught and a result is returned, so the client side doesn't see the exception.

One way to fix that is to remove the catch clause, and allow the exception to be thrown to the client side.

If you choose to do that, it's best practise to format your exception into something that a user can understand, and to remove stack trace / inner exception information so that the user won't be able to see the inner workings of your application.

Sign up to request clarification or add additional context in comments.

2 Comments

its works im remove try and catch blocks but how can i remove stack trace / inner exception information?
you can, for instance, catch the exception, and throw a new exception. that way you control the message, and the new exception doesn't contain the stack trace of the original exception. but take note to somehow log the original exception so that you would know xactly what went wrong.
0

How does jQuery know that the message you are sending back is an error? Because you are handling (also known as swallowing) the exception, you need to set the http response code to 500 to tell jQuery that the action failed by adding the following line in your exception handling code

HttpContext.Current.Response.StatusCode = 500

4 Comments

As I said in the answer, add a line in the catch exception block saying "Response.StatusCode = 500" this will set the status code.
in vb.net web service method cant set Response.StatusCode = 500
According to the MSDN docs you can... msdn.microsoft.com/en-us/library/…
You need to access it via the HttpContext object. So the code will be "HttpContext.Current.Response.StatusCode = 500"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.