Warm tip: This article is reproduced from stackoverflow.com, please click
react-native-android

How to fetch Webservice which takes string parameter?

发布于 2020-03-27 10:21:47

I try to fetch webservice method. However, it takes string. How can I send string parameter to webservice ?

I tried like this :

 fetch('url/MethodName', {
        method: 'POST',
        headers: new Headers({
                      Accept: 'application/json',
                    'Content-Type': 'application/json', 
            }),
        })

where should I put string parameter ?

Questioner
hakan
Viewed
20
Bob White 2019-07-03 22:27

by mozilla

var url = 'https://example.com/profile';
var data = {username: 'example'};

fetch(url, {
  method: 'POST', // or 'PUT'
  body: JSON.stringify(data), // data can be `string` or {object}!
  headers:{
    'Content-Type': 'application/json'
  }
}).then(res => res.json())
.then(response => console.log('Success:', JSON.stringify(response)))
.catch(error => console.error('Error:', error));

send Json data as parameter