-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAJAXREQUEST.HTML
114 lines (74 loc) · 3.29 KB
/
AJAXREQUEST.HTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<html>
<head>
<script>
function displayData(arr) {
pt = "<table border = '2px'><tr><th>id</th><th> userid</th><th>title<th>completed</th>";
for (user of arr) {
pt += `<tr><td>${user.id}</td><td>${user.userId}</td><td>${user.title}</td><td>${user.completed}</td></tr>`
}
pt += "</table>";
document.getElementById("mypid").innerHTML = pt;
}
function displaydetails(ob) {
pt = "Id : " + ob.id + "<br>";
pt += "user ID : " + ob.userId + "<br>" + "Title: " + ob.title + "<br>" + "status : " + ob.completed;
document.getElementById("mypid2").innerHTML = pt;
}
function getdetailsbyid()
{
var ps = new XMLHttpRequest();
var id = document.getElementById("id").value;
var url = "https://jsonplaceholder.typicode.com/todos/" + id
console.log(url);
ps.open("GET", "https://jsonplaceholder.typicode.com/todos/" + id, true);
ps.onreadystatechange = function() {
if (ps.readyState == 4 && ps.status == 200) {
alert(ps.responseText);
ob = JSON.parse(ps.responseText);
displaydetails(ob);
}
}
ps.send();
}
function linkdata() { // function call // first step now to create httprequest object
// first step now to create httprequest object
alert("Hello");
var ps;
if (window.XMLHttpRequest) // it will check that whether our browser support this request or not
{
ps = new XMLHttpRequest(); // if support then if statement execute if not then else statement will active the browser
} else {
ps = new ActivexObject("Microsoft.XMLHTTP")
}
// 2 . To generate ajax request
ps.open("GET", "https://jsonplaceholder.typicode.com/todos", true);
// 3. confrigur the url
ps.onreadystatechange = function() {
if (ps.readyState == 4 && ps.status == 200) {
alert(ps.responseText);
// json to javascript
ob = JSON.parse(ps.responseText);
console.log(ps.responseText);
document.getElementById("mypid").innerHTML = ps.responseText; // once the data will be come than display it
displayData(ob);
} else {
console.log(ps.responseText);
}
}
// send the request to display data function
ps.send();
}
</script>
</head>
<body>
<form>
id<input type="text" name="id" , id="id">
<button type="button" name="btn1" , id="btn1" onclick="linkdata()">linkdata</button>
<button type="button" name="btn" id="btn2" onclick="getdetailsbyid()">get data by id</button>
</form>
<br>
<br>
<div id="mypid"></div>
<div id="mypid2" style="margin-left:500px;border:2px solid blue ; width:400px;height:400px;border-radius:10px"></div>
</body>
</html>