在vb.net中获取xhr对象,而ajax调用失败 [英] Get xhr object in vb.net while ajax calling fails

查看:165
本文介绍了在vb.net中获取xhr对象,而ajax调用失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 jQuery.ajax 调用中有一个很大的问题。每当点击更新按钮,我都会调用Web服务。我有一个单独的Web服务类,其中包含几个方法。当我调用Web服务方法时,我已经进行了错误处理并在db中记录了错误信息,之后我必须重写ex,这意味着错误对象为 XMLHttpRequest 。可以在 VB.NET 中将 SqlException 分配给ajax对象( xhr ) / code>?请帮助我对我更有用。

I have a big problem in jQuery.ajax call. I am calling the web service whenever click the update button. I have a separate web service class, in which consist of few methods. When I calling the web service method, I have made the error handling and log the error information in db after that I have to override the "ex" that means error object to XMLHttpRequest. Is it possible to assign the SqlException to ajax object (xhr) in VB.NET? Please help me its much more useful for me.

推荐答案

是的,可以!我尝试在VB.NET中描述它(大部分我使用C#,但我希望我不会产生语法错误)。让我们有一个Web服务

Yes it is possible! I try to describe it in VB.NET (mostly I use C#, but I hope I'll not made syntax errors). Let us we have a Web service

<WebMethod()> _
<ScriptMethodAttribute(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True)> _
Public Function GetData(ByVal Age As Integer) As String
If Age <= 0 Then
    Throw(New ArgumentException("The parameter age must be positive."))
End If
'... some code
End Function

相同C#中的代码看起来像

The same code in C# look like

[WebMethod]
[ScriptMethod (UseHttpGet=true)]
public string GetData(int age)
{
    if (age <= 0)
        throw new ArgumentException("The parameter age must be positive.");
    // some code
}

如果负值Age输入异常 ArgumentException 将被抛出(所有我解释的另一个异常,如 SqlException )。

In case of negative Age input the exception ArgumentException will be thrown (all what I explain stay the same for another exception like SqlException).

现在,您有一个使用 jQuery.ajax 的JavaScript代码来调用该服务。然后,您可以通过以下方式扩展代码来支持异常处理:

Now you have a JavaScript code which use jQuery.ajax to call the service. Then you can expand the code to support the exception handling in the following way:

$.ajax({
    type: "GET",
    url: "MyWebService.asmx/GetData",
    data: {age: -5},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data, textStatus, xhr) {
        // use the data
    },
    error: function(xhr, textStatus, ex) {
        var response = xhr.responseText;
        if (response.length > 11 && response.substr(0, 11) === '{"Message":' &&
            response.charAt(response.length-1) === '}') {

            var exInfo = JSON.parse(response);
            var text = "Message=" + exInfo.Message + "\r\n" +
                       "Exception: " + exInfo.ExceptionType;
                      // + exInfo.StackTrace;
            alert(text);
        } else {
            alert("error");
        }
    }
});

如果抛出异常,我们会收到有关JSON格式错误的信息。我们反序列化到具有消息 ExceptionType StackTrace 属性,然后显示如下所示的错误消息

In case of exception be thrown, we receive information about error in JSON format. We deserialize it to the object having Message, ExceptionType and StackTrace property and then display error message like following

Message: The parameter age must be positive.
Exception: System.ArgumentException

在实际的应用程序中,您可能永远不会显示 StackTrace 属性。最重要的信息在消息中:异常文本和 ExceptionType :异常名称(如 System.ArgumentException System.Data.SqlClient.SqlException )。

In a real application you will probably never displayed the value of the StackTrace property. The most important information are in the Message: the text of exception and in ExceptionType: the name of exception (like System.ArgumentException or System.Data.SqlClient.SqlException).

这篇关于在vb.net中获取xhr对象,而ajax调用失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆