12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- // изначальный вариант решения с async/await fetch - сам не знаю зачем сделал)
- /*const jsonPost = async (url, data) => {
- try {
- const response = await fetch(url, {
- method: 'POST',
- body: JSON.stringify(data)
- });
- if (!response.ok) {
- new Error(`An error has occurred: ${response.status}`);
- } else {
- return JSON.parse(await response.text());
- }
- } catch (e) {
- return e;
- }
- }
- const url = "http://students.a-level.com.ua:10012";
- sendButton.addEventListener('click', async () => {
- let nickValue = nickInput.value;
- let messageValue = messageInput.value;
- const newPost = await jsonPost(url, {
- func: 'addMessage',
- nick: nickValue,
- message: messageValue
- }
- );
- console.log(newPost.nextMessageId);
- });*/
- const jsonPost = (url, data) => {
- return fetch(url, {
- method: 'POST',
- body: JSON.stringify(data)
- }).then(response => {
- if (response.ok) {
- return response.json();
- } else {
- throw new Error(`An error has occurred: ${response.status}`);
- }
- }).catch(err => {
- console.log(err);
- });
- }
- const url = "http://students.a-level.com.ua:10012";
- sendButton.addEventListener('click', () => {
- let nickValue = nickInput.value;
- let messageValue = messageInput.value;
- const newPost = jsonPost(url, {
- func: 'addMessage',
- nick: nickValue,
- message: messageValue
- }
- );
- newPost.then(data => console.log(data.nextMessageId))
- });
|