如何使用JSON在JavaScript中处理Web服务的XML响应数据

时间:2012-04-15 09:48:04

标签: javascript xml web-services

我是处理XML响应数据的新手。我有一个Web服务,它在SQL Server数据库中检查用户和他的密码,并相应地返回响应。 Web服务方法的代码如下;

<WebMethod()> _
    Public Function Authentication(ByVal username As String, ByVal password As String) As String

        'Public Function ConnectToSQL() As String
        Dim con As New SqlConnection
        Dim result As Boolean
        Dim response As String

        Try

            con.ConnectionString = "Data Source=TestServer;Initial Catalog=MyDB;Persist Security Info=True;User ID=myuser;Password=mypass"
            Dim cmd As New SqlCommand("SELECT username FROM user_detail WHERE username='" + username  + "' AND password='" + password + "'", con)
            con.Open()


            ' Execute Query
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            result = reader.HasRows
        'Validate user info from database 
           If result = True Then
            response = "Valid user info..Thanks"
        Else
            response = " Not valid user info..Please Enter again, Thanks"
        End If

        If Not reader Is Nothing Then
            reader.Close()
            End If

        Catch ex As Exception
            MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
        Finally
            con.Close() 'Whether there is error or not. Close the connection.
        End Try
        Return response 
    End Function
End Class

响应在XML数据中,如下图所示(如果输入有效的用户信息)

Valid Response

现在我想在Javascript中创建一个视图,它从用户获取输入,然后通过此Web服务从数据库验证用户infor。有人可以帮我怎么做吗?

1 个答案:

答案 0 :(得分:1)

您可以创建一个可以接受用户名和密码的表单

并使用javascript SOAP客户端提交到您的Web服务。因此您需要发出SOAP请求并将用户名和密码传递给它验证的服务。您可以在此处看到使用javascript消费Web服务的示例。

Simplest SOAP example

http://www.guru4.net/articoli/javascript-soap-client/en/

希望这会对你有所帮助。