我有一个VB.Net webmethod,只会抛出一个非描述性的"错误处理请求"当返回的行数增加时。在方法调试中,我看不到服务器端抛出的任何错误。但是,如果我使用Fiddler重新发出命令同样的错误显示,所以我认为它不是我的AJAX调用,而是我的webmethod。此时我不确定如何获取有关错误的更多详细信息,因为服务器代码不会引发错误。
webmethod运行SQL查询,然后调用函数以HTML表格式格式化结果。 HTML表稍后会添加到使用DataTables进行格式化的页面中。它可以正常工作50或行但大约100行它会引发错误。我可以验证这一点,因为第一个查询返回每个输入parm提供1行,允许我测试50,75和100行,直到我发现错误。
此时我不确定如何调试它。我无法了解如何获得有关可能原因的更多详细信息。
<System.Web.Services.WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function GetReportData2(ByVal sRptId As String, ByVal sParms1 As String, ByVal sParms2 As String) As String
'for some reason the parms on the screen come in with spaces as dividers
sParms1 = sParms1.Replace(" ", ",").Replace(vbCr, ",").Replace(vbLf, ",")
sParms2 = sParms2.Replace(" ", ",").Replace(vbCr, ",").Replace(vbLf, ",")
Dim dt = New DataTable()
Try
Select Case sRptId
Case "1"
Using conn = New SqlConnection(ConfigurationManager.ConnectionStrings("DirectoryInfoConnectionString").ConnectionString)
Using cmd = New SqlCommand("s_rpt_BatchLookup_People", conn)
cmd.CommandType = CommandType.StoredProcedure
'set timeout to 60s
cmd.CommandTimeout = 60
cmd.Parameters.Add("@input", SqlDbType.VarChar, -1).Value = sParms1
Using adapter = New SqlDataAdapter(cmd)
adapter.Fill(dt)
End Using
End Using
End Using
Case "2"
Using conn = New SqlConnection(ConfigurationManager.ConnectionStrings("DirectoryInfoConnectionString").ConnectionString)
Using cmd = New SqlCommand("s_rpt_BatchLookup_Client_Detail", conn)
cmd.CommandType = CommandType.StoredProcedure
'set timeout to 120s
cmd.CommandTimeout = 120
cmd.Parameters.Add("@input", SqlDbType.VarChar, -1).Value = sParms1
cmd.Parameters.Add("@cym", SqlDbType.VarChar, -1).Value = sParms2
Using adapter = New SqlDataAdapter(cmd)
adapter.Fill(dt)
End Using
End Using
End Using
Case Else
End Select
Return ConvertToHtmlFile2(dt)
Catch ex As Exception
Return ex.Message + ex.InnerException.ToString()
End Try
End Function
Public Shared Function ConvertToHtmlFile2(ByVal targetTable As DataTable) As String
Dim myHtmlFile As String = ""
If (targetTable Is Nothing) Then
Throw New System.ArgumentNullException("targetTable")
Else
'Continue.
End If
Dim myBuilder As System.Text.StringBuilder = New System.Text.StringBuilder()
myBuilder.Append("<thead><tr>")
For Each myColumn As DataColumn In targetTable.Columns
myBuilder.Append("<th>")
myBuilder.Append(myColumn.ColumnName)
myBuilder.Append("</th>")
Next myColumn
myBuilder.Append("</tr></thead>")
'add body
myBuilder.Append("<tbody>")
'Add the data rows.
For Each myRow As DataRow In targetTable.Rows
myBuilder.Append("<tr>")
For Each myColumn As DataColumn In targetTable.Columns
myBuilder.Append("<td>")
myBuilder.Append(myRow(myColumn.ColumnName).ToString())
myBuilder.Append("</td>")
Next myColumn
myBuilder.Append("</tr>")
Next myRow
'Close tags.
myBuilder.Append("</tbody>")
'Get the string for return.
myHtmlFile = myBuilder.ToString()
Return myHtmlFile
End Function
这是来自Fiddler的原始错误数据。我认为这不会有所帮助。
HTTP/1.1 500 Internal Server Error
Server: ASP.NET Development Server/11.0.0.0
Date: Sun, 13 Sep 2015 20:23:55 GMT
X-AspNet-Version: 4.0.30319
jsonerror: true
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 91
Connection: Close
{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
更新1 我将webmethod移动到一个单独的asmx文件以进行更多测试。我还创建了一个简单的查询来从表中选择X行数。再一次,&#34;内部服务器错误500&#34;仅当行数超过某个限制时才会发生。 但直接运行asmx不会引发相同的错误 。只有AJAX POST请求似乎会导致错误,因为行数超过了某个限制。
我读到的一切都说这必须是服务器端,但如果我隔离了asmx文件,它一切正常。我无法看到AJAX post调用在一定数量的行之后导致错误的错误。
答案 0 :(得分:0)
这最终成为了没有在web.config中设置maxJsonLength的问题。由于自定义错误模式打开,我没有收到消息。不确定为什么,但在webmethod本身设置最大值也无济于事。但我读过其他帖子说必须在网络方法中完成。最后这很难找到,因为AJAX调用的错误部分在responseText字段中没有详细的消息告诉我错误的详细信息。
有关错误消息丢失的详细信息,请参阅this post on customError mode and AJAX responseText