我真的无法理解这里的确切问题在使用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是空的。
按钮点击代码:
<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
但是这个Web服务代码工作正常,根据我的知识问题是ajax代码,任何人都可以请帮助我。 欢呼声。
答案 0 :(得分:3)
您是否尝试将要求的网址直接插入浏览器... 您将收到一条错误消息,告诉您,
“只能在脚本”
中调用类定义中具有[ScriptService]属性的Web服务将[ScriptService]属性添加到服务类定义的顶部,它可以解决您的问题。
我有类似的问题,它对我有用:D
答案 1 :(得分:0)
将数据类型从json更改为文本修复了我的问题