Click here to Skip to main content
15,881,172 members
Articles / Web Development / ASP.NET

Synchronous JavaScript call using Scriptable XML Webservice (S-AJAX)

Rate me:
Please Sign up or sign in to vote.
4.58/5 (15 votes)
4 Nov 2008CPOL2 min read 79.2K   346   33  
Call an ASP.NET AJAX Webservice or page methods synchronously from client-side JavaScript to use in a custom validator.
<%@ Page Language="C#" AutoEventWireup="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
    function GetSynchronousJSONResponse(url, postData) {
        var xmlhttp = null;
        if (window.XMLHttpRequest)
            xmlhttp = new XMLHttpRequest();
        else if (window.ActiveXObject) {
            if (new ActiveXObject("Microsoft.XMLHTTP"))
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            else
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }

        url = url + "?rnd=" + Math.random(); // to be ensure non-cached version

        xmlhttp.open("POST", url, false);
        xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        xmlhttp.send(postData);
        var responseText = xmlhttp.responseText;
        return responseText;
    }

    function HelloWorld() {
        var result = GetSynchronousJSONResponse('<%= Page.ResolveUrl("~/WebService.asmx/HelloWorld") %>', null);
        result = eval('(' + result + ')');
        alert(result.d);
    }
    function HelloMe() {
        var result = GetSynchronousJSONResponse('<%= Page.ResolveUrl("~/WebService.asmx/HelloMe") %>', '{"Name":"' + document.getElementById('txtName').value + '"}');
        result = eval('(' + result + ')');
        alert(result.d);
    }
    function CheckForDuplicateInDB(sender, arg) {
        var result = GetSynchronousJSONResponse('<%= Page.ResolveUrl("~/WebService.asmx/CheckName") %>', '{"Name":"' + arg.Value + '"}');
        result = eval('(' + result + ')');
        arg.IsValid = !result.d;
    }
    function CallError() {
        var result = GetSynchronousJSONResponse('<%= Page.ResolveUrl("~/WebService.asmx/CallError") %>', null);
        result = eval('(' + result + ')');
        if (typeof (result.d) == 'undefined')
            alert(result.Message);
        else
            alert(result.d);
    }
    </script>
</head>
<body>
    <form runat="server">
    <dl>
        <dt>Simple Call</dt>
        <dt><a href="javascript:HelloWorld()">Hello World</a></dt>
        <dt><hr /></dt>
        <dt>Call With Argument</dt>
        <dt><input id="txtName" value="Your Name" />
            <a href="javascript:HelloMe()">Hello Me</a>
        </dt>
        <dt><hr /></dt>
        <dt>Call With Custom Validator</dt>
        <dt>
            <asp:TextBox ID="txtCheckName" runat="server" Text="Validator"></asp:TextBox>
            <asp:CustomValidator ID="cvName" ControlToValidate="txtCheckName" EnableClientScript="true" ClientValidationFunction="CheckForDuplicateInDB" runat="server" Display="Static" ToolTip="Name is Duplicate." Text="*"></asp:CustomValidator>
            <asp:Button ID="btnClickMe" runat="server" Text="ClickMe!" />
        </dt>
	<dt><hr /></dt>
        <dt>How to handle server Error</dt>
        <dt><a href="javascript:CallError()">Call Error</a></dt>
        <dt><hr /></dt>
    </dl>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer Gateway Technolabs
India India
ASP.net C#, Microsoft Dynamics CRM 4.0

Comments and Discussions