showing results for - "call the web api from 24 ajax 28 29 function "
Tim
20 Sep 2020
11- Calling the web api method without parameter
2•	If  you do not have parameter in your web API method then you no need to pass the data parameter inside AJAX function.
3•	The URL parameter of ajax function must match with the Route attribute of web api method.
4
5o	Your web api controller action method.
6       
7       [HttpGet]
8        [Route("student/Get")]
9        public IEnumerable<string> Get()
10        {
11            return new string[] { "student1", "student2" };
12        }
13
14o	Your ajax function
15$('#btnBindDropDown').on('click', function () {
16                $('#btnBindDropDown').click(function () {
17                    $.ajax({
18                        type: 'GET',
19                        url: 'student/Get',
20                        contentType: 'application/json; charset=utf-8',
21                        dataType: 'json',
22                        success: function (data) {
23                            alert('data is ' + data);
24                        },
25                        error: function () {
26                            alert("INSIDE FAILURE");
27                        }
28                    });
29                });
30                
31            });
32
332-	Now calling the web api method with  parameter
34
35
36o	Your web api controller action method.
37
38 [HttpGet]
39        [Route("student/GetWithId")]
40        public int Get(int Id)
41        {
42            return Id;
43        }
44
45o	Your ajax function
46
47The Id property must match with web api method parameter.
48
49
50$.ajax({
51                        type: 'GET',
52                        url: 'student/GetWithId',
53                        data: { Id: 101 },
54                        contentType: 'application/json; charset=utf-8',
55                        dataType: 'json',
56                        success: function (data) {
57                            alert('data is ' + data);
58                        },
59                        error: function () {
60                            alert("INSIDE FAILURE");
61                        }
62                    });
63
643-	calling the web api method by passing object 
65
66o	Your web api controller action method.
67
68 [HttpPost]
69        [Route("api/student/names1")]
70        public Employee GetEmployee(Employee employee)
71        {
72            employee.Id = 101;
73            employee.Name = "Kaushal";
74            return employee; 
75  }
76
77o	Your ajax function
78The object nameemployeemust match with the web api method parameter
79The URL name can be anything , But both should match.
80i.e  Route attribute of web api method and URL parameter of ajax function.
81If you are passing object then you will use JSON.stringfy function.
82If webApi method is not returning anything then the parameter  dataType: 'json', 
83Is optinal. This parameter is required if webapi method is returning something and you want to do something with the returned data.
84
85$('#btnBindDropDown').click(function ()
86            {
87                var employee = { Id: 101, Name: "Love singh" };  
88                    $.ajax({
89                        type: 'POST',
90                        url: 'api/student/names1',
91                        data: JSON.stringify(employee),
92                        contentType: 'application/json; charset=utf-8',
93                        dataType: 'json',
94                        success: function (data) {
95                            alert('data is ' + data);
96                        },
97                        error: function () {
98                            alert("INSIDE FAILURE");
99                        }
100                    });
101                
102            });
103
Nicolás
03 Jan 2017
1BY LOVE
21- Calling the web api method without parameter
3•	If  you do not have parameter in your web API method then you no need to pass the data parameter inside AJAX function.
4•	The URL parameter of ajax function must match with the Route attribute of web api method.
5
6o	Your web api controller action method.
7       
8       [HttpGet]
9        [Route("student/Get")]
10        public IEnumerable<string> Get()
11        {
12            return new string[] { "student1", "student2" };
13        }
14
15o	Your ajax function
16$('#btnBindDropDown').on('click', function () {
17                $('#btnBindDropDown').click(function () {
18                    $.ajax({
19                        type: 'GET',
20                        url: 'student/Get',
21                        contentType: 'application/json; charset=utf-8',
22                        dataType: 'json',
23                        success: function (data) {
24                            alert('data is ' + data);
25                        },
26                        error: function () {
27                            alert("INSIDE FAILURE");
28                        }
29                    });
30                });
31                
32            });
33
342-	Now calling the web api method with  parameter
35
36
37o	Your web api controller action method.
38
39 [HttpGet]
40        [Route("student/GetWithId")]
41        public int Get(int Id)
42        {
43            return Id;
44        }
45
46o	Your ajax function
47
48The Id property must match with web api method parameter.
49
50
51$.ajax({
52                        type: 'GET',
53                        url: 'student/GetWithId',
54                        data: { Id: 101 },
55                        contentType: 'application/json; charset=utf-8',
56                        dataType: 'json',
57                        success: function (data) {
58                            alert('data is ' + data);
59                        },
60                        error: function () {
61                            alert("INSIDE FAILURE");
62                        }
63                    });
64
653-	calling the web api method by passing object 
66
67o	Your web api controller action method.
68
69 [HttpPost]
70        [Route("api/student/names1")]
71        public Employee GetEmployee(Employee employee)
72        {
73            employee.Id = 101;
74            employee.Name = "Kaushal";
75            return employee; 
76  }
77
78o	Your ajax function
79The object nameemployeemust match with the web api method parameter
80The URL name can be anything , But both should match.
81i.e  Route attribute of web api method and URL parameter of ajax function.
82If you are passing object then you will use JSON.stringfy function.
83If webApi method is not returning anything then the parameter  dataType: 'json', 
84Is optinal. This parameter is required if webapi method is returning something and you want to do something with the returned data.
85
86$('#btnBindDropDown').click(function ()
87            {
88                var employee = { Id: 101, Name: "Love singh" };  
89                    $.ajax({
90                        type: 'POST',
91                        url: 'api/student/names1',
92                        data: JSON.stringify(employee),
93                        contentType: 'application/json; charset=utf-8',
94                        dataType: 'json',
95                        success: function (data) {
96                            alert('data is ' + data);
97                        },
98                        error: function () {
99                            alert("INSIDE FAILURE");
100                        }
101                    });
102                
103            });
queries leading to this page
call api with out ajaxajax api callintegrating web api using ajaxajax 3a function 28data 2c callback 29 examplehow to api call with ajaxusing jquery ajax to retrieve data from web api net corehow to call api using ajax in jqueryusing ajax for an api call get web api url ajax 24 ajax http request examplecall webapi htm ajaxjquery ajax get example web api in javascripthow to make ajax call in web apirest api call ajaxapi call using ajaxhow to call the web api from 24 ajax 28 29 function what is ajax api callweb api using ajax net core sql serverhow to call web api in ajaxhow to get jquery ajax response objectcreate api for ajax callajax request api examplecall web api using jquery ajaxcall web api using jquery ajax in asp net corehow to make api request in ajax rest api ajax callcan we write jquery ajax api call reques inside functionapi call with ajaxjquery ajax api call exampleapi ajax callweb api core ajax loadcall api using jquery ajaxcall jquery on ajax responseajax call requestcall api using ajax javascripthow to make an ajax call to a http serverajax api call exampleusing ajax from an apiweb api call using ajaxapi call in ajaxcall the web api from 24 ajax 28 29 function how to call ajax request from websitemake ajax request from apirequest api ajaxajax api callscall api with ajaxapi calls using ajaxjquery ajax api calljs ajax api callapi call with jquery ajaxjquery ajax call web apiajax javascript with apicall web api in only in jquery ajaxcalling an api through ajaxapi request using ajaxajax call api examplecall api using ajax jqueryuse api with ajaxcall an api using ajax call in asp net mvccall function in ajax responsejquery call api inside ajaxcompleteajax call to get web apijquery ajax request to a web apimake an ajax request to your apihow to call web api post method using ajaxajax api call jquery exampleajax calls apisaccess ajax response datajquery ajax call api examplemaking an ajax request to an apiis ajax an apiasp net mvc jquery ajax web apihow to call api in jquery ajaxcall rest api using jquery ajaxreceive data in api from ajax in c 23ajax call to api jqueryajax call to web apiajax call api in jqueryncs gov inweb api with ajax 3a understand get verb in restful web apiajax and rest apiajax request inside ajax requestweb api api ajax mvchow to call api using ajax in jquery and ajaxcalling api using ajaxwhat does jquery ajax do with get requestsjavascript ajax call to rest apiajax method apicall api using ajax 24 ajax call appcontrollerhow to call web api using ajax callfunction 28response 29 ajaxhow to call web api using 24 ajaxajax through api callhow to call an api with ajaxjquery ajax response functionweb api ajax 24 ajax api callhow to make ajax call to an apicalling web api from jquery ajaxcall a web api in ajax net core sql servercall web api from ajaxhow to call web api from ajaxtype ajax api responsehow to call ajax in web apijquery ajax call web api sucess returnrequest api with ajax requestajax api call in htmlajax call using apiajax request apiajax api requestajax api call examplejavascript api in place of ajaxajax jquery api callhow to ajax request apijavascript ajax call for get apijquery ajax call to an apimake an ajax request apicall web api from ajax jqueryajax call and api call is different 3fjavascript ajax call to apiusing ajax to call an apihow to handle ajax call response in jquerycall rest api from ajaxajax request on apiajax request to apihow to call api using ajaxhow to call web api using jquery ajax in htmljquery ajax call to an web apicall ajax apihow does ajax return an api callmake ajax request to my api asp netrest api ajaxajax from apiajax response functionexecute ajax request calling a web apijquery ajax call apihow to call web api in jquery ajaxweb api post method printing html pageapi ajax requesthow to pass api call with javascript ajax jqueryajax call web apicalling rest api using ajaxajax call to apicall api in ajaxhow to call api using ajax in javascriptajax how to call apimake api call javascript ajaxapis ajax callsuse ajax to call apiweb api call in ajaxhow to get a response from an api with ajax call in jqueryajax to call apiajax request callajax apijquery api call ajax callapi call api jquery ajaxjquery ajax calll rest apiajax call apiajax with apimaking ajax api callsajax url apicall web api in jquery ajaxajax web apiajax for api callconsume web api using ajax callsajax calls to web apicall api ajaxhow make an ajax request to your apiajax code call api examplehow to do ajax api callajax rest api callajax request with apiajax call for apiajax call api urlajax api call js examplehow to get a the response from an ajax call in a libraryajax api call jquerycall the web api from 24 ajax 28 29 function