在使用 ajax 使用 JavaScript 调用 html 页面中的 Web 服务时,我真的不明白这里的确切问题是什么,因为它会产生以下错误:
加载资源失败:服务器响应状态为 500(内部服务器错误)
阿贾克斯代码:
function Image() {
$.ajax({
type: "POST",
url: "WebService.asmx/GetImage",
data: "{'sDB': '" + "sDB" + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnGetMemberSuccess,
failure: function (errMsg) {
$('#errorMessage').text(errMsg); //errorMessage is id of the div
}
});
function OnGetMemberSuccess(data, status) {
alert("data" + data.d);
$("#MemberDetails").html(data.d);
$('input[type=button]').attr('disabled', false);
}
}
其中 sDB 为空。
按钮点击代码:
<input type="button" id="Button" value="Image" onclick="Image()" />
我在以前的项目中使用了相同的代码,但它工作正常。
网络服务代码:
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <WebMethod()> _
Public Function GetImage()
Dim cmd As SqlCommand = New SqlCommand
Dim con = New SqlConnection("server = PROG19-PC;database = XIDBViews;Trusted_Connection = yes")
cmd.Connection = con
con.Open()
Dim strQuery As String = ""
Dim oSB As New StringBuilder
Dim table As New Table()
Dim tr As New TableRow()
Dim td As New TableCell()
Dim sFirstNameValue As String = String.Empty
Dim sLastNameValue As String = String.Empty
Dim DoBValue As String = String.Empty
Dim sPhoto As String = String.Empty
strQuery = "SELECT [sFirstName],[sLastName],[DoB],[sPhoto] FROM [XIDBViews].[dbo].[tblEmployee] "
cmd = New SqlCommand(strQuery, con)
cmd.ExecuteNonQuery()
Dim dr As SqlDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
oSB.Append("<table><thead><tr><th>" + " FirstName" + "</th><th>" + "Lastname" + "</th><th>" + "DoB" + "</th><th>" + "Party" + "</th><th>" + "Photo" + "</th></tr></thead>")
While dr.Read()
sFirstNameValue = dr("sFirstName").ToString
sLastNameValue = dr("sLastName").ToString
DoBValue = dr("DoB").ToString
sPhoto = dr("sPhoto").ToString
oSB.Append("<tbody id=tbodyid'>")
oSB.Append("<tr>")
oSB.Append("<td class=border1>")
oSB.Append(sFirstNameValue)
oSB.Append("</td>")
oSB.Append("<td class=border1 >")
oSB.Append(sLastNameValue)
oSB.Append("</td>")
oSB.Append("<td class=border1>")
oSB.Append(DoBValue)
oSB.Append("</td>")
oSB.Append("<td class=border1>")
oSB.Append(sPhoto)
oSB.Append("</td>")
oSB.Append("</tr>")
oSB.Append("</tbody>")
End While
dr.Close()
con.Close()
MsgBox(oSB.ToString)
'Debug.Print(oSB.ToString)
Return oSB.ToString()
End Function
End Class
但是这个网络服务代码工作正常,据我所知,问题出在 ajax 代码上,任何人都可以帮我解决这个问题。干杯。