Testing API with AJAX

7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€43.99
List Price:  €61.60
You save:  €17.60
£37.42
List Price:  £52.39
You save:  £14.97
CA$69.15
List Price:  CA$96.82
You save:  CA$27.66
A$78.18
List Price:  A$109.46
You save:  A$31.28
S$65.30
List Price:  S$91.43
You save:  S$26.12
HK$387.73
List Price:  HK$542.86
You save:  HK$155.12
CHF 41.24
List Price:  CHF 57.75
You save:  CHF 16.50
NOK kr518.26
List Price:  NOK kr725.61
You save:  NOK kr207.34
DKK kr328.44
List Price:  DKK kr459.85
You save:  DKK kr131.40
NZ$84.43
List Price:  NZ$118.21
You save:  NZ$33.78
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.45
৳6,075.37
List Price:  ৳8,506
You save:  ৳2,430.63
₹4,224.25
List Price:  ₹5,914.29
You save:  ₹1,690.04
RM215.75
List Price:  RM302.06
You save:  RM86.31
₦80,136.96
List Price:  ₦112,198.16
You save:  ₦32,061.20
₨14,052.18
List Price:  ₨19,674.18
You save:  ₨5,622
฿1,670.16
List Price:  ฿2,338.36
You save:  ฿668.20
₺1,924.49
List Price:  ₺2,694.45
You save:  ₺769.95
B$281.58
List Price:  B$394.24
You save:  B$112.65
R930.55
List Price:  R1,302.84
You save:  R372.29
Лв86.06
List Price:  Лв120.49
You save:  Лв34.43
₩71,075.59
List Price:  ₩99,511.52
You save:  ₩28,435.92
₪181.62
List Price:  ₪254.28
You save:  ₪72.66
₱2,791.29
List Price:  ₱3,908.03
You save:  ₱1,116.73
¥7,152.05
List Price:  ¥10,013.44
You save:  ¥2,861.39
MX$976.77
List Price:  MX$1,367.56
You save:  MX$390.78
QR182.01
List Price:  QR254.83
You save:  QR72.82
P682.36
List Price:  P955.36
You save:  P273
KSh6,461.20
List Price:  KSh9,046.20
You save:  KSh2,585
E£2,539.18
List Price:  E£3,555.06
You save:  E£1,015.87
ብር6,591.18
List Price:  ብር9,228.18
You save:  ብር2,637
Kz45,590.88
List Price:  Kz63,830.88
You save:  Kz18,240
CLP$47,237.55
List Price:  CLP$66,136.35
You save:  CLP$18,898.80
CN¥364.67
List Price:  CN¥510.57
You save:  CN¥145.90
RD$2,933.33
List Price:  RD$4,106.90
You save:  RD$1,173.57
DA6,626.94
List Price:  DA9,278.25
You save:  DA2,651.30
FJ$113.07
List Price:  FJ$158.31
You save:  FJ$45.24
Q385.11
List Price:  Q539.19
You save:  Q154.07
GY$10,461.12
List Price:  GY$14,646.40
You save:  GY$4,185.28
ISK kr6,419.71
List Price:  ISK kr8,988.11
You save:  ISK kr2,568.40
DH461.54
List Price:  DH646.19
You save:  DH184.65
L858.12
List Price:  L1,201.44
You save:  L343.32
ден2,706.43
List Price:  ден3,789.22
You save:  ден1,082.79
MOP$399.48
List Price:  MOP$559.31
You save:  MOP$159.82
N$929.36
List Price:  N$1,301.18
You save:  N$371.82
C$1,840.11
List Price:  C$2,576.31
You save:  C$736.19
रु6,768.73
List Price:  रु9,476.77
You save:  रु2,708.03
S/183.28
List Price:  S/256.61
You save:  S/73.33
K206.10
List Price:  K288.57
You save:  K82.46
SAR187.50
List Price:  SAR262.52
You save:  SAR75.01
ZK1,396.33
List Price:  ZK1,954.98
You save:  ZK558.64
L219.02
List Price:  L306.65
You save:  L87.62
Kč1,095.88
List Price:  Kč1,534.32
You save:  Kč438.44
Ft17,793.41
List Price:  Ft24,912.20
You save:  Ft7,118.79
SEK kr482.10
List Price:  SEK kr674.98
You save:  SEK kr192.88
ARS$58,235.07
List Price:  ARS$81,533.76
You save:  ARS$23,298.69
Bs344.69
List Price:  Bs482.60
You save:  Bs137.90
COP$211,058.07
List Price:  COP$295,498.18
You save:  COP$84,440.11
₡25,282.35
List Price:  ₡35,397.31
You save:  ₡10,114.96
L1,290.42
List Price:  L1,806.69
You save:  L516.27
₲398,584.89
List Price:  ₲558,050.74
You save:  ₲159,465.85
$U2,104.70
List Price:  $U2,946.75
You save:  $U842.04
zł187.87
List Price:  zł263.03
You save:  zł75.16
Already have an account? Log In

Transcript

Hello, guys, welcome to the 19 session of the ASP. NET Web API. In this session, we will test the our API that we created in a previous session with the AJAX request. For this let's switch to the Visual Studio. Let's add before I'm going to add the new folder I am editing the new HTML page with the title so all in that page you first of all we need to add the reference to the jQuery which was present in the script folder. Drag and drop the link to the HTML page.

I am creating a button, the ID btn. So, and the text so data and one deal with the ID result. And now I'm going to write the jQuery code for sending the AJAX request. Opening the script tag. Specify the script type that is a JavaScript. When the document is ready, we need to call the anonymous function.

On which reo right do a handler for the btn. So on click renew to call type function in which we are originated sending Ajax requests by the jQuery. Within that function we need to specify the configuration for always extra question. Start with the URL. For attendee URL, click on the solution. Select the project and press the f4 which will switch to the property window from which select the URL within the single code, paste it and append the API slash controller name that is an employee semicolon the type of request that we are going to file that will be get in our case and the data type that will be written in the our case it will be a JSON and write the handler for the error wonder that anonymous function will be a call which will show the error message in do our research do and then come and create the success function to success function we solicited data.

When we are receiving the data successfully, then we need to loop through the each record and display the name of the employee as well as the HR department within the braces. For that we need to first of all create a variable that holds your formatted data. Were formatted data equal to empty string and look through each data pass to the anonymous function which take the index and value as a parameter. For each value, we need to append it to the formatted data am using backcourt dollar sign curly braces on and off value dot name space the department name within the braces for that dollar curly braces valued out the par meant and use the br tag for a newline and a semicolon after the loop we need to append. After the loop we need to set the formatted data to the our result view for that to use the s for that use the jQuery selector dot dot html and specify the formatted data Let's we'll do a solution.

Let's click on the source data. It's slowly undefined in the case of the department name, and the braces are coming the next line, let's modify it. Let's write the department name instead of the HR department and complete the bracket after that, instead of the new line, save it and reload the our page. And let's click again. You can see that it returned the proper output. If you click on the inspect element and open the Network tab, like when you click on DSR data, it will send the XR and from that you can notice that the responses tight which will populate it by the word jQuery.

First of all, many To use the jQuery Ajax function for sending the request, in which we need to specify the URL from which we need to read, we need to specify we need to we need to use the AJAX function of the jQuery in which we need to specify the URL from where we are going to review the data. And the specify the meta type that is the get post put or delete and specify the written data type. That can be either Jason XML or the apple index to within the error function we can write a code to handle the over error. Within the success we are going to manipulate the our data. Thanks for watching. Have a nice day.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.