Web services 在vb.net中获取xhr对象,而ajax调用失败
我在调用jQuery.ajax时遇到了一个大问题。每当单击“更新”按钮时,我都会调用web服务。我有一个单独的web服务类,其中包含几个方法。调用web服务方法时,我已经进行了错误处理,并将错误信息记录在db中,之后我必须将表示错误对象的“ex”重写为Web services 在vb.net中获取xhr对象,而ajax调用失败,web-services,exception-handling,jquery,error-handling,asmx,Web Services,Exception Handling,Jquery,Error Handling,Asmx,我在调用jQuery.ajax时遇到了一个大问题。每当单击“更新”按钮时,我都会调用web服务。我有一个单独的web服务类,其中包含几个方法。调用web服务方法时,我已经进行了错误处理,并将错误信息记录在db中,之后我必须将表示错误对象的“ex”重写为XMLHttpRequest。是否可以将SqlException分配给VB.NET中的ajax对象(xhr)?请帮助我它对我更有用 是的,这是可能的!我尝试在VB.NET中描述它(我主要使用C#,但我希望不会出现语法错误)。让我们有一个Web服务
XMLHttpRequest
。是否可以将SqlException
分配给VB.NET
中的ajax对象(xhr
)?请帮助我它对我更有用 是的,这是可能的!我尝试在VB.NET中描述它(我主要使用C#,但我希望不会出现语法错误)。让我们有一个Web服务
<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
在抛出异常的情况下,我们会收到JSON格式的错误信息。我们将其反序列化为具有Message
、ExceptionType
和StackTrace
属性的对象,然后显示如下错误消息
Message: The parameter age must be positive.
Exception: System.ArgumentException
在实际应用程序中,您可能永远不会显示
StackTrace
属性的值。最重要的信息在消息
:异常文本和异常类型
:异常名称(如系统.ArgumentException
或系统.Data.SqlClient.SqlException
)。不走运。更改URL并没有更改错误,而使用您的错误代码,我只会收到一个警告,上面写着“错误”。但是谢谢@OrenA:我的答案中的代码被简化为只显示web服务抛出异常时的错误详细信息。您在中描述的错误具有完全不同的性质。您有textStatus=**parseerror**。因此,您应该首先使用alert(xhr.responseText)显示xhr.responseText
代码>
$.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");
}
}
});
Message: The parameter age must be positive.
Exception: System.ArgumentException