【问题标题】:jQuery error function not call asp.netjQuery错误函数不调用asp.net
【发布时间】:2011-10-12 09:56:09
【问题描述】:

我将 Jquery 与 Asp.net Web 服务一起使用,我有一个表单我使用 jquery ajax 功能将数据传递给 Web 服务

我的问题是,当我在 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()> _
<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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 它的工作原理是删除 try 和 catch 块,但如何删除堆栈跟踪/内部异常信息?
    • 例如,您可以捕获异常并抛出新异常。这样您就可以控制消息,并且新异常不包含原始异常的堆栈跟踪。但请注意以某种方式记录原始异常,以便您确切知道出了什么问题。
    【解决方案2】:

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

    HttpContext.Current.Response.StatusCode = 500
    

    【讨论】:

    • 正如我在答案中所说,在 catch 异常块中添加一行“Response.StatusCode = 500”,这将设置状态码。
    • 在 vb.net 网络服务方法不能设置 Response.StatusCode = 500
    • 根据 MSDN 文档,您可以... msdn.microsoft.com/en-us/library/…
    • 你需要通过 HttpContext 对象来访问它。所以代码将是“HttpContext.Current.Response.StatusCode = 500”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 2011-12-21
    • 1970-01-01
    相关资源
    最近更新 更多