Saturday 16 July 2016

Ajax Calling in jQuery..

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Viftech.WebForm1" %>

<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="Common.js"></script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="button" id="btnAdd" value="Click me" />
            <input type="text" id="txtNum" />
        </div>
    </form>
</body>
</html>

<script type="text/javascript">
    var rootURL = "<%=ResolveUrl("WebForm1.aspx/")%>"

    $("#btnAdd").click(function () {
        var url = rootURL + "LoadMe";
        var textVal = $("#txtNum").val();
        AjaxCall(url,textVal);
    });

    function AjaxCall(url, textVal) {
        $.ajax({
            url: url,
            type: 'POST',
            cache: true,
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: "{'entity': '" + textVal + "'}",
            async: false,
            success: function (result) {
                debugger
                var Bresponse = eval("(" + result.d + ")");
                alert(Bresponse);
            },
            error: function (xhr, textStatus, errorThrown) {
                alert('request failed');
            }
        });
    }

</script>


/////Code Behind////

      [System.Web.Services.WebMethod]
        public static int LoadMe(int entity)
        {

            int a = 0;
            int c = entity + 100;
            return c;

        }




////////////////////////////// Ajax Calling with Json Data///////////////////////////////

 var textVal = $("#txtNum").val();
        var obj = new Object();
        obj.ID = textVal;


function AjaxCall(url, textVal) {
        $.ajax({
            url: url,
            type: 'POST',
            cache: true,
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: "{'entity': '" + JSON.stringify(textVal) + "'}",
            async: false,
            success: function (result) {
                debugger
                var Bresponse = eval("(" + result.d + ")");
                alert(Bresponse);
            },
            error: function (xhr, textStatus, errorThrown) {
                alert('request failed');
            }
        });
    }


////code behind//////

 public class abc {

            public int ID { get; set; }

        }

[System.Web.Services.WebMethod]
        public static int LoadMe(string entity)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            abc defaultdata = (abc)js.Deserialize(entity, typeof(abc));

            int c = defaultdata.ID;

            return 1;
        }




No comments:

Post a Comment