简体   繁体   中英

Error in calling POST api in react native by handling action in redux

i am fetching some data by using POST api call in which i have data and a token value for header, but i am getting bad response and i checked many docs but can't figure out the error, here is the code:

export const shareUserProfileHandler = (sharedReceiverData) => {
    return dispatch => {
        let formData = new FormData();
        for (let key in sharedReceiverData) {
            formData.append(key, sharedReceiverData[key]);
        }
         let requestConfig = {
             method: 'POST',
             headers: {
                 'Accept': 'application/json',
                 'Content-Type': 'multipart/form-data',
                 'Authorization': 'Token 97a74c03004e7d6b0658b14ddb'
             },
             body: formData
         };

        fetch(`http://api.com`, requestConfig)
        .then(response => response.json())
        .then(response => {
            alert('share user card api worked')

        })
        .catch(error => {
            alert('api error ' + error)
        })
    }
};

the above is catching error and showing - SyntaxError: JSON Parse error: Unrecognized token'<'

Your response doesn't seem to be a JSON.

Replace

.then((response) => response.json())

For

.then((response) => { console.log('response', response); response.json() })

And check what is wrong with the response before the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM