在使用ajax使用JavaScript调用html页面中的web服务时,我真的无法理解这里的确切问题,因为它会产生以下错误:
加载资源失败:服务器响应状态为500(内部服务器错误)
Ajax代码:
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为Null。
按钮点击代码:
<input type="button" id="Button" value="Image" onclick="Image()" />
我在以前的项目中使用了相同的代码,但工作正常。
Web服务代码:
<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代码,有人能帮我吗?干杯。
您是否尝试将您请求的URL直接插入浏览器...您将收到一条错误消息,告诉您,
“只能从脚本调用类定义中具有[ScriptService]属性的Web服务”
将[ScriptService]属性添加到服务类定义的顶部,它可能会解决您的问题。
我有一个类似的问题,它对我有效: D
将数据类型从json更改为文本修复了我的问题