Call the web api from $.ajax() function.

1- Calling the web api method without parameter
•	If  you do not have parameter in your web API method then you no need to pass the data parameter inside AJAX function.
•	The URL parameter of ajax function must match with the Route attribute of web api method.

o	Your web api controller action method.
       
       [HttpGet]
        [Route("student/Get")]
        public IEnumerable<string> Get()
        {
            return new string[] { "student1", "student2" };
        }

o	Your ajax function
$('#btnBindDropDown').on('click', function () {
                $('#btnBindDropDown').click(function () {
                    $.ajax({
                        type: 'GET',
                        url: 'student/Get',
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        success: function (data) {
                            alert('data is ' + data);
                        },
                        error: function () {
                            alert("INSIDE FAILURE");
                        }
                    });
                });
                
            });

2-	Now calling the web api method with  parameter


o	Your web api controller action method.

 [HttpGet]
        [Route("student/GetWithId")]
        public int Get(int Id)
        {
            return Id;
        }

o	Your ajax function

•	The Id property must match with web api method parameter.


$.ajax({
                        type: 'GET',
                        url: 'student/GetWithId',
                        data: { Id: 101 },
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        success: function (data) {
                            alert('data is ' + data);
                        },
                        error: function () {
                            alert("INSIDE FAILURE");
                        }
                    });

3-	calling the web api method by passing object 

o	Your web api controller action method.

 [HttpPost]
        [Route("api/student/names1")]
        public Employee GetEmployee(Employee employee)
        {
            employee.Id = 101;
            employee.Name = "Kaushal";
            return employee; 
  }

o	Your ajax function
•	The object name ‘employee’ must match with the web api method parameter
•	The URL name can be anything , But both should match.
i.e  Route attribute of web api method and URL parameter of ajax function.
•	If you are passing object then you will use JSON.stringfy function.
•	If webApi method is not returning anything then the parameter  dataType: 'json', 
Is optinal. This parameter is required if webapi method is returning something and you want to do something with the returned data.

$('#btnBindDropDown').click(function ()
            {
                var employee = { Id: 101, Name: "Love singh" };  
                    $.ajax({
                        type: 'POST',
                        url: 'api/student/names1',
                        data: JSON.stringify(employee),
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        success: function (data) {
                            alert('data is ' + data);
                        },
                        error: function () {
                            alert("INSIDE FAILURE");
                        }
                    });
                
            });

0
8
Cecilia 120 points

                                    BY LOVE
1- Calling the web api method without parameter
&bull;	If  you do not have parameter in your web API method then you no need to pass the data parameter inside AJAX function.
&bull;	The URL parameter of ajax function must match with the Route attribute of web api method.

o	Your web api controller action method.
       
       [HttpGet]
        [Route(&quot;student/Get&quot;)]
        public IEnumerable&lt;string&gt; Get()
        {
            return new string[] { &quot;student1&quot;, &quot;student2&quot; };
        }

o	Your ajax function
$('#btnBindDropDown').on('click', function () {
                $('#btnBindDropDown').click(function () {
                    $.ajax({
                        type: 'GET',
                        url: 'student/Get',
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        success: function (data) {
                            alert('data is ' + data);
                        },
                        error: function () {
                            alert(&quot;INSIDE FAILURE&quot;);
                        }
                    });
                });
                
            });

2-	Now calling the web api method with  parameter


o	Your web api controller action method.

 [HttpGet]
        [Route(&quot;student/GetWithId&quot;)]
        public int Get(int Id)
        {
            return Id;
        }

o	Your ajax function

&bull;	The Id property must match with web api method parameter.


$.ajax({
                        type: 'GET',
                        url: 'student/GetWithId',
                        data: { Id: 101 },
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        success: function (data) {
                            alert('data is ' + data);
                        },
                        error: function () {
                            alert(&quot;INSIDE FAILURE&quot;);
                        }
                    });

3-	calling the web api method by passing object 

o	Your web api controller action method.

 [HttpPost]
        [Route(&quot;api/student/names1&quot;)]
        public Employee GetEmployee(Employee employee)
        {
            employee.Id = 101;
            employee.Name = &quot;Kaushal&quot;;
            return employee; 
  }

o	Your ajax function
&bull;	The object name &lsquo;employee&rsquo; must match with the web api method parameter
&bull;	The URL name can be anything , But both should match.
i.e  Route attribute of web api method and URL parameter of ajax function.
&bull;	If you are passing object then you will use JSON.stringfy function.
&bull;	If webApi method is not returning anything then the parameter  dataType: 'json', 
Is optinal. This parameter is required if webapi method is returning something and you want to do something with the returned data.

$('#btnBindDropDown').click(function ()
            {
                var employee = { Id: 101, Name: &quot;Love singh&quot; };  
                    $.ajax({
                        type: 'POST',
                        url: 'api/student/names1',
                        data: JSON.stringify(employee),
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        success: function (data) {
                            alert('data is ' + data);
                        },
                        error: function () {
                            alert(&quot;INSIDE FAILURE&quot;);
                        }
                    });
                
            });

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
using ajax to call an api how to call api using ajax in jquery and ajax how to api call with ajax how to call web api in ajax make ajax request from api ajax call web api call function in ajax response ajax code call api example how to handle ajax call response in jquery web api call using ajax how to call api using ajax in jquery call ajax api how to get a response from an api with ajax call in jquery consume web api using ajax calls function(response) ajax api ajax call api call with jquery ajax javascript ajax call to api apis ajax calls jquery ajax call api example api ajax request call web api from ajax jquery ajax request to api call api with out ajax ajax api call js example ajax jquery api call request api with ajax request ajax response function Calling rest api using ajax request api ajax js ajax api call ajax call to api ajax request api example jquery ajax response function ajax call to api jquery Make an AJAX request API jquery ajax calll rest api how to call api using ajax api call in ajax how to do ajax api call ajax request with api jquery ajax api call example ajax web api jquery ajax call api ajax api call jquery example rest api ajax call ajax api calls ajax call using api api call with ajax ajax request call ajax call api call jquery on ajax response jquery ajax call web api sucess return call rest api from ajax web api ajax javascript ajax call to rest api ajax api request ajax method api make api call javascript ajax javascript ajax call for get api $.ajax api call how to call api in jquery ajax ajax request api how to make ajax call to an api ajax to call api AJAX call API example ajax api call jquery call api using ajax call api with ajax how to get a the response from an ajax call in a library ajax calls apis ajax calls to web api api call api jquery ajax ajax api call example call api using jquery ajax use ajax to call api Api call using ajax call api using ajax jquery ajax api call using AJAX for an API call using ajax from an API ajax how to call api how to pass api call with javascript ajax jquery web api api ajax mvc make ajax request to my api asp net web api core ajax load how to make ajax call in web api web api post method printing html page web api call in ajax call webapi htm ajax Ncs.gov.inWeb API With AJAX: Understand get Verb in Restful Web API jquery ajax get example web api in javascript call web api using jquery ajax how to call web api from ajax calling web api from jquery ajax call web api using jquery ajax in asp net core web api using ajax net core sql server call a web api in ajax net core sql server call an api using ajax call in asp.net mvc how to call web api using jquery ajax in html using jquery ajax to retrieve data from web api .Net Core how to call web api in jquery ajax jquery ajax request to a web api $.ajax call appcontroller integrating web api using ajax jquery ajax call to an web API how to call web api post method using ajax get web api url ajax call web api from ajax jquery ajax call web api receive data in api from ajax in c# asp.net mvc jquery ajax web api how to call ajax in web api how to call web api using ajax call ajax call to get web api How to call web api using $.ajax how to call the web api from $.ajax() function. Call the web api from $.ajax() function.
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source