【发布时间】:2013-04-16 07:15:03
【问题描述】:
在使用 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()" />
我在以前的项目中使用了相同的代码,但它工作正常。
网络服务代码:
<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 代码,任何人都可以帮我解决这个问题。 干杯。
【问题讨论】:
-
客户端的javascript错误不会导致500,因为正如代码状态所示,这是一个内部服务器错误。您的服务器端一定有问题。
-
data: "{'sDB': '" + "sDB" + "'}"这一行将结束为data:"{'sDB': 'sDB'}",字面意思。你真的是这个意思吗? -
感谢您的回复,我试过了,但它不适合我。
-
不,我在问/通知您,
data部分可能无法按您的预期工作,字符串 literal 真的是您想要的吗? -
是的。我尝试将值传递给 Web 服务中的查询,它对我有用,所以我尝试了静态方式。
标签: javascript ajax vb.net web-services