1
0

vueSubject.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. subjects:[],
  29. },
  30. methods: {
  31. showSubjects() {
  32. console.log(this.subjects)
  33. },
  34. async getSubjects(){
  35. this.subjects = await requestFunc("/subject/", "GET")
  36. },
  37. },
  38. mounted() {
  39. this.getSubjects()
  40. }
  41. });
  42. addSubject = () =>{
  43. let name = document.querySelector('.subjectName').value
  44. let shortName = document.querySelector('.shortName').value
  45. var select = document.getElementById("selectProfmodule");
  46. var value = select.value;
  47. let type = document.querySelector('.type').checked
  48. var myHeaders = new Headers();
  49. myHeaders.append("Content-Type", "application/json");
  50. var raw = {"Name":name,"ID_professionalmodule":Number(value),"Shortname": shortName, "IDStype": Number(type)};
  51. requestFunc(url="/subject/", method="POST", data=raw)
  52. alert("Добавлено")
  53. }