Skip to content

Commit 72e77c6

Browse files
author
Roman Tsukanov
committed
Use Fetch API
1 parent 66e272b commit 72e77c6

File tree

5 files changed

+80
-45
lines changed

5 files changed

+80
-45
lines changed

get-started-library/angular/src/app/app.component.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,21 @@ export class AppComponent implements OnInit {
3939
}
4040

4141
// function saveSurveyResults(url, json) {
42-
// const request = new XMLHttpRequest();
43-
// request.open('POST', url);
44-
// request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
45-
// request.addEventListener('load', () => {
46-
// // Handle "load"
42+
// fetch(url, {
43+
// method: 'POST',
44+
// headers: {
45+
// 'Content-Type': 'application/json;charset=UTF-8'
46+
// },
47+
// body: JSON.stringify(json)
48+
// })
49+
// .then(response => {
50+
// if (response.ok) {
51+
// // Handle success
52+
// } else {
53+
// // Handle error
54+
// }
55+
// })
56+
// .catch(error => {
57+
// // Handle error
4758
// });
48-
// request.addEventListener('error', () => {
49-
// // Handle "error"
50-
// });
51-
// request.send(JSON.stringify(json));
5259
// }

get-started-library/jquery/index.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,21 @@ $(function() {
3030
});
3131

3232
// function saveSurveyResults(url, json) {
33-
// const request = new XMLHttpRequest();
34-
// request.open('POST', url);
35-
// request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
36-
// request.addEventListener('load', () => {
37-
// // Handle "load"
33+
// fetch(url, {
34+
// method: 'POST',
35+
// headers: {
36+
// 'Content-Type': 'application/json;charset=UTF-8'
37+
// },
38+
// body: JSON.stringify(json)
39+
// })
40+
// .then(response => {
41+
// if (response.ok) {
42+
// // Handle success
43+
// } else {
44+
// // Handle error
45+
// }
46+
// })
47+
// .catch(error => {
48+
// // Handle error
3849
// });
39-
// request.addEventListener('error', () => {
40-
// // Handle "error"
41-
// });
42-
// request.send(JSON.stringify(json));
4350
// }

get-started-library/knockout/index.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,21 @@ document.addEventListener("DOMContentLoaded", function() {
3232
});
3333

3434
// function saveSurveyResults(url, json) {
35-
// const request = new XMLHttpRequest();
36-
// request.open('POST', url);
37-
// request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
38-
// request.addEventListener('load', () => {
39-
// // Handle "load"
35+
// fetch(url, {
36+
// method: 'POST',
37+
// headers: {
38+
// 'Content-Type': 'application/json;charset=UTF-8'
39+
// },
40+
// body: JSON.stringify(json)
41+
// })
42+
// .then(response => {
43+
// if (response.ok) {
44+
// // Handle success
45+
// } else {
46+
// // Handle error
47+
// }
48+
// })
49+
// .catch(error => {
50+
// // Handle error
4051
// });
41-
// request.addEventListener('error', () => {
42-
// // Handle "error"
43-
// });
44-
// request.send(JSON.stringify(json));
4552
// }

get-started-library/react/src/App.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,23 @@ function App() {
3838
}
3939

4040
// function saveSurveyResults(url, json) {
41-
// const request = new XMLHttpRequest();
42-
// request.open('POST', url);
43-
// request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
44-
// request.addEventListener('load', () => {
45-
// // Handle "load"
41+
// fetch(url, {
42+
// method: 'POST',
43+
// headers: {
44+
// 'Content-Type': 'application/json;charset=UTF-8'
45+
// },
46+
// body: JSON.stringify(json)
47+
// })
48+
// .then(response => {
49+
// if (response.ok) {
50+
// // Handle success
51+
// } else {
52+
// // Handle error
53+
// }
54+
// })
55+
// .catch(error => {
56+
// // Handle error
4657
// });
47-
// request.addEventListener('error', () => {
48-
// // Handle "error"
49-
// });
50-
// request.send(JSON.stringify(json));
5158
// }
5259

5360
export default App;

get-started-library/vue/src/components/MyFirstSurvey.vue

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,22 @@ export default {
5050
}
5151
5252
// function saveSurveyResults(url, json) {
53-
// const request = new XMLHttpRequest();
54-
// request.open('POST', url);
55-
// request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
56-
// request.addEventListener('load', () => {
57-
// // Handle "load"
53+
// fetch(url, {
54+
// method: 'POST',
55+
// headers: {
56+
// 'Content-Type': 'application/json;charset=UTF-8'
57+
// },
58+
// body: JSON.stringify(json)
59+
// })
60+
// .then(response => {
61+
// if (response.ok) {
62+
// // Handle success
63+
// } else {
64+
// // Handle error
65+
// }
66+
// })
67+
// .catch(error => {
68+
// // Handle error
5869
// });
59-
// request.addEventListener('error', () => {
60-
// // Handle "error"
61-
// });
62-
// request.send(JSON.stringify(json));
6370
// }
6471
</script>

0 commit comments

Comments
 (0)