2016-02-01 20 views
6

Si verificano problemi durante l'utilizzo di FETCH.React Native - La richiesta POST di recupero viene inviata come richiesta GET

Sto provando a fare una richiesta POST utilizzando FETCH in react-native.

fetch("http://www.example.co.uk/login", { 
     method: 'POST', 
     headers: { 
      'Accept': 'application/json', 
      'Content-Type': 'application/json' 
     }, 
     body: JSON.stringify({ 
      username: 'test', 
      password: 'test123', 
     }) 
    }) 

     .then((response) => response.json()) 
     .then((responseData) => { 
      console.log(
       "POST Response", 
       "Response Body -> " + JSON.stringify(responseData) 
      ) 
     }) 
     .done(); 
} 

Quando ho ispezionare questa chiamata con Charles è registrato come una richiesta GET e il nome utente e la password che dovrebbe essere nel corpo non sono lì.

enter image description here

chiunque può aiutare con questo problema?

risposta

2

Ho avuto lo stesso tipo di problema. Devi assegnare l'oggetto, non so perché.

let options = {};

options.body = formdata;

options.header = intestazione;

options.method = 'post';

+1

Questo non ha alcun senso. Ciò comporterebbe esattamente lo stesso oggetto .. – Tieme

+0

forse è in conflitto con questa nuova funzionalità di ES6? http://www.2ality.com/2011/11/keyword-parameters.html – Kokizzu

3

Questo ha funzionato per me:

let data = { 
    method: 'POST', 
    credentials: 'same-origin', 
    mode: 'same-origin', 
    body: JSON.stringify({ 
    appoid: appo_id 
    }), 
    headers: { 
    'Accept':  'application/json', 
    'Content-Type': 'application/json', 
    'X-CSRFToken': cookie.load('csrftoken') 
    } 
} 
return fetch('/appointments/get_appos', data) 
     .then(response => response.json()) // promise 
     .then(json => dispatch(receiveAppos(json))) 
} 
3

Ho avuto questo problema quando la richiesta POST è stato quello di un HTTPS (piuttosto che HTTP) del server. Per qualche motivo, sarebbe da qualche parte lungo il percorso essere convertito in una richiesta GET.

Si scopre che ciò che stavo facendo in modo errato era inviare la richiesta a http://myserver.com:80 anziché a https://myserver.com:443. Una volta passato al prefisso e alle porte appropriate, la richiesta sarebbe stata inviata correttamente come POST.

+0

La stessa cosa è accaduta. –

Problemi correlati