`Why does VK API return code 200 and undefined?
E
E
Egor Varenik2021-02-09 23:18:48
JavaScript
Egor Varenik, 2021-02-09 23:18:48

Why does VK API return code 200 and undefined?

let connection = new XMLHttpRequest();
    connection.open('GET', `${ prefix }groups.getLongPollServer?group_id=202228658${ postfix }`);
    connection.responseType = 'json';
    connection.send();
    const connection_timer = setTimeout(() => {
      console.log('ERROR! Shit happened! Server connection timeout!');
    }, 5000)
    connection.onload = () => {
      clearTimeout(connection_timer);
      console.log(connection.status);
      console.log(connection.response);
    }
    connection.onerror = () => {
      console.log('ERROR! Shit happened! Bad server connection!');
    }


Return code 200 but connection.response = undefined. Why is that?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor Varenik, 2021-02-10
@egovar

Problem solved.
1) Instead of connection.response, you should have used connection.responseText
2) After that, error 15 was returned, but this is my oversight - to receive longpoll server vk data, you need to create an access key with the right to manage the community

K
Kirill, 2021-02-09
@kustiktm_kirill

Isn't it easier to use the vk-io library to work with the VK API?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question