stage6.js 679 B

12345678910111213141516171819202122232425262728293031
  1. const jsonPost = async (url, data) => {
  2. try {
  3. const response = await fetch(url, {
  4. method: 'POST',
  5. body: JSON.stringify(data)
  6. });
  7. if (!response.ok) {
  8. new Error(`An error has occurred: ${response.status}`);
  9. } else {
  10. return JSON.parse(await response.text());
  11. }
  12. } catch (e) {
  13. return e;
  14. }
  15. }
  16. const url = "http://students.a-level.com.ua:10012";
  17. sendButton.addEventListener('click', async () => {
  18. let nickValue = nickInput.value;
  19. let messageValue = messageInput.value;
  20. const newPost = await jsonPost(url, {
  21. func: 'addMessage',
  22. nick: nickValue,
  23. message: messageValue
  24. }
  25. );
  26. console.log(newPost.nextMessageId);
  27. });