Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
Can anybody tell me how to call web service in HTML page.
I have one method in my web service like
C#
public bool Check(string S,string R)
{
        if(S==R)
                return true;
        else
                return false;
}

I would like to call this method in my HTML page.
I think we can achieve this by using Java script or else by jQuery.
If it possible in these two ways can anybody explain this with example.
Posted
Updated 23-Feb-12 3:10am
v2
Comments
André Kraak 23-Feb-12 9:10am    
Edited question:
Added pre tags
Spelling/Grammar

hi,

There are some syntax errors with the JavaScript you posted. First, let look at the WebService, we need to include [System.Web.Script.Services.ScriptService] To allow this Web Service to be called from script. Let do a simple test
C#
[System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService
    {

        [WebMethod(Description = "Validate the login credentials")]
        public bool Validate(string UserName, string Password)
        {
            if (Password == "test" && UserName == "test")
                return true;
            else
                return false;
        }
    }


Then the JavaScript on your service.html page should look like

XML
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        var Surl = "http://localhost:3031/HTMLService/Service.asmx/Validate";
        $(document).ready(function () {
            $("input#submit").click(function (event) {
                var uid = document.getElementById("UserName").value;
                var pwd = document.getElementById("Password").value;
                var dataString = "{ 'UserName' : '" + uid + "', 'Password' : '" + pwd + "'}";

                $.ajax({
                    ServiceCallID: 1,
                    url: Surl,
                    type: 'POST',
                    data: dataString,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",

                    success: function (result) {
                        returnVal = result.d;
                        alert(returnVal);
                    },

                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        returnVal = '';
                    }
                });
            });
        });

</script>
 
Share this answer
 
Hi,

I am unable to get it..

Please find the code as below & resolve the issue

service.cs
<code>[WebMethod(Description = "Validate the login credentials")]
public bool Validate(string UserName,string Password)
{
SqlConnection oCon = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
SqlCommand oCmd = new SqlCommand("select LoginPassword from tbl_LoginInfo where UserName='"+UserName+"'", oCon);
oCon.Open();
string Temp = oCmd.ExecuteScalar().ToString();
if (Password == Temp)
return true;
else
return false;
}</code>
service.html
<code>
<script type="text/javascript" language="javascript">
var Surl="http://localhost:3031/HTMLService/Service.asmx";
$(document).ready(function() {
$("input#submit").click(function(event) {
$.ajax(
{ ServiceCallID = 1;
var uid = document.getElementById("UserName").value;
var pwd = document.getElementById("Password").value;
var dataString = "{ 'UserName' : '" + uid + "', 'Password' : '" + pwd + "'}";
url: Surl;
type: 'POST';
data: dataString,
contentType: "application/json; charset=utf-8";
dataType: "json";
success: function (result) {
returnVal = result.d;
};
error: function (XMLHttpRequest, textStatus, errorThrown) {
returnVal = '';
};
});
}}

</script>
</head>
<body onload="Initialize()" id="service" style="behavior:url(webservice.htc)" onresult="Result()">
<form action="">
<label>Enter the Username</label>
<input type="text" id="UserName" name="username" />
<label>Enter the Password</label>
<input type="text" id="Password" name="password" />
<input id="submit" type="button" value="button" />
</form>
</body>
</code>
 
Share this answer
 
Comments
André Kraak 23-Feb-12 9:08am    
If you have a question about or comment on a given solution use the "Have a Question or Comment?" option beneath the solution. When using this option the person who gave the solution gets an e-mail message and knows you placed a comment and can respond if he/she wants.

Please move the content of this solution to the solution you are commenting on and remove the solution.
Thank you.
You use .ajax[^]

Example:
JavaScript
var dataString = "{'Site':'" + site + "'}";
$.ajax(
 {
     url: wsUrl,
     type: 'POST',
     data: dataString,
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (result) {
         returnVal = result.d;
     },
     error: function (XMLHttpRequest, textStatus, errorThrown) {
         returnVal = '';
     }
 });
 }


But, you'll also have to mark your C# function as a WebMethod. You've got a lot of work to do so come back with something more specific if you get stuck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900