jQuery错误函数不能调用asp.net

时间:2011-10-12 09:56:10

标签: javascript jquery asp.net asp.net-mvc

即时通讯使用Jquery和Asp.net网络服务,我有一个表格即时通讯使用jquery ajax功能将数据传递给网络服务

我的问题是当我在webmethod中注释一个sqlparameter它会给我一个异常,但这不会调用我的jquery onError函数,它总是(虽然异常与否)调用OnSuccess函数

它是什么原因?

我使用Newtonsoft序列化对json格式的响应

请帮助我 这是我的客户端脚本

<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>

所以这是我的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 个答案:

答案 0 :(得分:1)

这是因为您的网络方法中有çatch子句 捕获异常并返回结果,因此客户端不会看到异常。

解决这个问题的一种方法是删除catch子句,并允许将异常抛出到客户端。

如果您选择这样做,最好将异常格式化为用户可以理解的内容,并删除堆栈跟踪/内部异常信息,以便用户无法看到您的内部工作原理应用

答案 1 :(得分:0)

jQuery如何知道您发回的邮件是错误的?因为您正在处理(也称为吞咽)异常,所以需要将http响应代码设置为500,以通过在异常处理代码中添加以下行来告诉jQuery操作失败

HttpContext.Current.Response.StatusCode = 500