Monday, October 22, 2012

jQuery Ajax Text Response Example

jQuery supports different types of responses data types which are sent by the server as a result of processing Ajax request, the various types of data supported are HTML, XML, JSON and Text. In this post we shall see on how to process Text data returned from the server using jQuery.

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

$.ajax({
    url: "AjaxData.txt",
    cache: true,
    dataType: "text",
    success: function(result) {…}
});

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</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();
                $.ajax({
                    url: "AjaxData.txt",
                    cache: false,
                    dataType: "text",
                    success: function(result) {
                        $("#divText").text(result);
                    }
                });
            });
        });
    </script>                 
</head>
<body>
    <form id="frmAjax" runat="server">
    <table border="1">
        <tr>
            <td><b>Action</b></td>
            <td><b>Response</b></td>
        </tr>
        <tr>
            <td>
                <asp:Button
                    ID="btnGetText"
                    runat="server"
                    Text="Get Text"/>
            </td>
            <td>
                <div id="divText"></div> 
            </td>
        </tr> 
    </table>
    </form>
</body>
</html>

AjaxData.txt (Server side)
This is a sample text placed in the server.
jQuery will load this text in the Browser using Ajax.

Run the Application and click on the btnGetText button, notice that the request is processed and the Text data sent by the server is parsed and displayed in the Browser.

Related Post
jquery ajax method
jquery ajax get example
jquery ajax post example
jquery ajax html response example
jquery ajax xml response example
jquery ajax json response example
jquery ajax text response example

Search Flipkart Products:
Flipkart.com

No comments: