Thursday, October 25, 2012

jQuery Load Text Response Example


The jQuery Load() ajax method supports HTML and Text type responses sent by the server. In this post we shall see on how to process Text data returned from the server using the jQuery Load() method.

The syntax for receiving and processing Text response is as follows.

$("#<Target HTML element ID>").load("<Server URL>");

The following example receives a Text format data from the server and displays the data in a DIV tag in the browser.



.aspx Page (Client Side)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>jQuery Ajax Load</title>
    <script type="text/javascript" src="JavaScript/jquery-1.7.2.js"></script>
    <script
        type="text/javascript"
        language="javascript">
        $(document).ready(function() {
            //
            // Get Text Response data from the Server
            $('#btnGetText').click(function(event) {
                event.preventDefault();
                $("#divText").load("AjaxData.txt");
            });
         
        });
    </script>                 
</head>
<body>
    <form id="frmAjax" runat="server">
    <table border="1">
        <tr>
            <td><b>Action</b></td>
            <td><b>Response</b></td>
        </tr>
            <tr valign="middle">
                <td>
                    <asp:Button
                        ID="btnGetText"
                        runat="server"
                        Text="Get Text" />
                </td>
                <td align="center">
                    <br /><div id="divtext"></div> 
                </td>
            </tr>           
    </table>
    </form>
</body>
</html>

.AjaxData.txt (Server Side)

This is a sample text placed in the server.

Search Flipkart Products:
Flipkart.com

No comments: