1
0

vue.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const requestFunc = async(url, method = "GET", data = null, token = null) => {
  2. apihost = 'http://schedule.tomtit.tomsk.ru/api'
  3. method = method.toLocaleUpperCase()
  4. let fullurl = `${apihost}${url}`;
  5. let options = {
  6. method: method,
  7. headers: {
  8. "Content-Type": "application/json",
  9. "Authorization": `Bearer ${token}`,
  10. },
  11. };
  12. switch(method) {
  13. case "PUT":
  14. delete options.headers["Content-Type"];
  15. options.body = data;
  16. break;
  17. case "POST": case "PATCH": case "DELETE":
  18. options.body = JSON.stringify(data);
  19. break;
  20. }
  21. const res = await fetch(fullurl, options);
  22. return await res.json();
  23. };
  24. var app = new Vue({
  25. el: '#app',
  26. delimiters: ['${', '}'],
  27. data: {
  28. classrooms:[],
  29. buildings: []
  30. },
  31. methods: {
  32. showClassrooms() {
  33. console.log(this.classrooms)
  34. },
  35. async getClassrooms(){
  36. this.classrooms = await requestFunc("/classroom", "GET")
  37. },
  38. async getBuildings() {
  39. const buildings = await requestFunc("/building/", "GET")
  40. .then(response => {
  41. console.log(response)
  42. this.buildings = response
  43. })
  44. },
  45. },
  46. mounted() {
  47. this.getClassrooms()
  48. this.getBuildings()
  49. }
  50. });