Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Wednesday, 27 July 2016

How to call web api using Ajax call

 <script>
        $(document).ready(function () {
            $("#Save").click(function () {

                var person = new Object();
                person.name = $('#name').val();
                person.surname = $('#surname').val();

                $.ajax({
                    url: 'http://localhost:11023/api/values/Get',
                    type: 'POST',
                    dataType: 'json',
                    data: person,
                    success: function (data, textStatus, xhr) {
                        console.log(data);
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        console.log('Error in Operation');
                    }
                });


            });
        });
    </script>

 Name :- <input type="text" name="name" id="name" />
        Surname:- <input type="text" name="surname" id="surname" />
        <input type="button" id="Save" value="Save Data" />



public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

Monday, 29 December 2014

How to call javascript and jquery function from code behind in ASP.net and c#

Introduction: Here I will tell you how you can call javascript and jquery function 
from codebehind.
Descirption:
Here I will tell you how you can call javascript and jquery function from codebehind.
If you are using scriptmanager on page then you can call
ScriptManager instance.
If you are not using script manager then you can call 
Page.ClientScript instance

Implementation:
In Code Behind 
protected void MyButton_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", 
 "MyFunction();", true);
} 

protected void MyButton_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this.GetType(), "myScript", 
  "MyFunction();", true);
} 

Thursday, 4 December 2014

Call web method from page using jquery and ajax without postback

Introduction: In this article I will show you how to call webmethod from aspx page using jquery
and ajax without postback.

Description:  Call webmethod from aspx page using jquery and ajax without postback.



In Aspx Page:

<head runat="server">
    <title></title>
<script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowTime() {
    $.ajax({
        type: "POST",
        url: "CS.aspx/GetTime",
        data: '{name: "' + $("#<%=txtName.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}
</script>
</head>

<body style = "font-family:Arial; font-size:10pt">
<form id="form1" runat="server">
<div>
Your Name :
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
    onclick = "ShowCurrentTime()" />
</div>
</form>
</body>


In Code Behind:

[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
    return "Hello " + name + Environment.NewLine + "The Current Time is: "
        + DateTime.Now.ToString();
}