B
B
Bogdan2017-07-19 13:36:22
JavaScript
Bogdan, 2017-07-19 13:36:22

AJAX fetch() and POST?

Hello. Do not prompt in what there can be a problem. When using fetch () for some reason, the request is sent not to the desired page, but to the main one. Thanks

const urlSend = '/admin/users/filter222';
    const typeSend = 'POST';

    // Вот так все работает как нужно
    const request = new XMLHttpRequest( );
    request.open( typeSend, urlSend, true );
    request.onload = event => console.log( event.currentTarget.responseText );
    request.send( );

    // здесь идет запрос и выдает весь HTML главной страницы в моем случае http://localhost:8084
    fetch( urlSend, { method: typeSend } )
      .then( response => response.text( ) )
      .then( text => console.log( text ) );

    // Так идет запрос на верный адрес, только на GET, а мне нужен POST
    fetch( urlSend )
      .then( response => response.text( ) )
      .then( text => console.log( text ) );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bogdan, 2017-07-19
@bogdan_uman

as it turned out, everything is because of cookies. That's how it works

fetch( urlSend, { method: typeSend, credentials: 'same-origin' } )
      .then( response => response.text( ) )
      .then( text => console.log( text ) );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question