Skip to content
Snippets Groups Projects
Commit ea696ba0 authored by Lucas Eduardo Schoenfelder's avatar Lucas Eduardo Schoenfelder
Browse files

fixed incorrect JSON parsing error

parent cfba72d2
Branches
Tags
6 merge requests!57Merge of develop into master,!56Fixed buttons reportar, seguir, compartilhar, guardar and entrar (in comments...,!39Update admin system,!32Homologa,!31Fix console error,!30Centraliza axios
......@@ -33,7 +33,6 @@ function fetchHeaders () {
if (auth_headers) {
const myHeaders = new Headers(auth_headers)
myHeaders.append('Content-Type', 'application/json')
return myHeaders
}
else {
......@@ -88,7 +87,9 @@ export const getRequest = (url, onSuccess, onError) => {
if (response.headers.has('access-token')) {
updateAccessToken(response.headers.get('access-token'))
}
return response.json()
return response.json().catch(err => {
return {};
})
})
.then(data => {
console.log(data)
......@@ -108,7 +109,9 @@ export const deleteRequest = (url, onSuccess, onError) => {
if (response.headers.has('access-token')) {
updateAccessToken(response.headers.get('access-token'))
}
return response.json()
return response.json().catch(err => {
return {};
})
})
.then(data => {
console.log(data)
......@@ -120,16 +123,26 @@ export const deleteRequest = (url, onSuccess, onError) => {
}
export const putRequest = (url, payload, onSuccess, onError) => {
let newHeaders = fetchHeaders()
if (payload instanceof FormData) {
newHeaders.append('Content-Type', 'multipart/form-data')
}
else {
newHeaders.append('Content-Type', 'application/json')
}
fetch((`${apiUrl}${url}`), {
method : 'PUT',
headers : fetchHeaders(),
body: JSON.stringify(payload)
headers : newHeaders,
body: payload instanceof FormData ? payload : JSON.stringify(payload)
})
.then(response => {
if (response.headers.has('access-token')) {
updateAccessToken(response.headers.get('access-token'))
}
return response.json()
return response.json().catch(err => {
return {};
})
})
.then(data => {
console.log(data)
......@@ -141,16 +154,26 @@ export const putRequest = (url, payload, onSuccess, onError) => {
}
export const postRequest = (url, payload, onSuccess, onError) => {
let newHeaders = fetchHeaders()
if (payload instanceof FormData) {
newHeaders.append('Content-Type', 'multipart/form-data')
}
else {
newHeaders.append('Content-Type', 'application/json')
}
fetch((`${apiUrl}${url}`), {
method : 'POST',
headers : fetchHeaders(),
body: JSON.stringify(payload)
headers : newHeaders,
body: payload instanceof FormData ? payload : JSON.stringify(payload)
})
.then(response => {
if (response.headers.has('access-token')) {
updateAccessToken(response.headers.get('access-token'))
}
return response.json()
return response.json().catch(err => {
return {};
})
})
.then(data => {
console.log(data)
......@@ -172,7 +195,9 @@ export const fetchAllRequest = (urls, onSuccess, onError) => {
updateAccessToken(res.headers.get('access-token'))
}
}
return Promise.all(responses.map( (response) => response.json()))
return Promise.all(responses.map( (response) => response.json().catch(err => {
return {};
})))
}).then( (data) => {
onSuccess(data)
}).catch((error) => {
......@@ -217,7 +242,9 @@ export const authentication = (url, payload, onSuccess, onError) => {
sessionStorage.setItem('@portalmec/auth_headers', JSON.stringify(auth_headers))
return response.json()
return response.json().catch(err => {
return {};
})
})
.then(data => {
console.log(data)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment