Minni Tomar Antil
Minni Tomar Antil

Reputation: 50

React Native - Parsing error from JSON Response

Here is my code.

I am calling the following function to get state list.

 callGetStatesApi()
 {
   callGetApi(GLOBAL.BASE_URL + GLOBAL.Get_States)
   .then((response) => {
           // Continue your code here...
           stateArray = result.data
           Alert.alert('Alert!', stateArray)
     });
 }

Here is common callGetApi function for getting response from GET Api.

export function callGetApi(urlStr, params) {
    return fetch(urlStr, {
            method: "GET",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(params)
        })
        .then((response) => response.json())
        .then((responseData) => {
            result = responseData
        })
        .catch((error) => {
             console.error(error);
             Alert.alert('Alert Title failure' + JSON.stringify(error))
        });
} 

I am getting the following error.

enter image description here

Upvotes: 0

Views: 999

Answers (1)

Arunkumar
Arunkumar

Reputation: 1741

Alert only show the string , but your case "stateArray" is complex object (array,structure..)

So use stateArray.toString() or JSON.stringify(stateArray),

Or

Try the below method and let me know,

fetch(GLOBAL.BASE_URL + GLOBAL.Get_States, {
    method: 'get',
    headers: { 'Accept': 'application/json','Content-Type': 'application/json',}
}).then((response) => response.json())
.then((responseData) => {
    console.log(responseData) // this is the response from the server
    // Continue your code here...
       stateArray = result.data
       Alert.alert('Alert!', stateArray)
}).catch((error) => {
   console.log('Error');
});

Upvotes: 1

Related Questions