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 () { ...@@ -33,7 +33,6 @@ function fetchHeaders () {
if (auth_headers) { if (auth_headers) {
const myHeaders = new Headers(auth_headers) const myHeaders = new Headers(auth_headers)
myHeaders.append('Content-Type', 'application/json')
return myHeaders return myHeaders
} }
else { else {
...@@ -88,7 +87,9 @@ export const getRequest = (url, onSuccess, onError) => { ...@@ -88,7 +87,9 @@ export const getRequest = (url, onSuccess, onError) => {
if (response.headers.has('access-token')) { if (response.headers.has('access-token')) {
updateAccessToken(response.headers.get('access-token')) updateAccessToken(response.headers.get('access-token'))
} }
return response.json() return response.json().catch(err => {
return {};
})
}) })
.then(data => { .then(data => {
console.log(data) console.log(data)
...@@ -108,7 +109,9 @@ export const deleteRequest = (url, onSuccess, onError) => { ...@@ -108,7 +109,9 @@ export const deleteRequest = (url, onSuccess, onError) => {
if (response.headers.has('access-token')) { if (response.headers.has('access-token')) {
updateAccessToken(response.headers.get('access-token')) updateAccessToken(response.headers.get('access-token'))
} }
return response.json() return response.json().catch(err => {
return {};
})
}) })
.then(data => { .then(data => {
console.log(data) console.log(data)
...@@ -120,16 +123,26 @@ export const deleteRequest = (url, onSuccess, onError) => { ...@@ -120,16 +123,26 @@ export const deleteRequest = (url, onSuccess, onError) => {
} }
export const putRequest = (url, payload, 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}`), { fetch((`${apiUrl}${url}`), {
method : 'PUT', method : 'PUT',
headers : fetchHeaders(), headers : newHeaders,
body: JSON.stringify(payload) body: payload instanceof FormData ? payload : JSON.stringify(payload)
}) })
.then(response => { .then(response => {
if (response.headers.has('access-token')) { if (response.headers.has('access-token')) {
updateAccessToken(response.headers.get('access-token')) updateAccessToken(response.headers.get('access-token'))
} }
return response.json() return response.json().catch(err => {
return {};
})
}) })
.then(data => { .then(data => {
console.log(data) console.log(data)
...@@ -141,16 +154,26 @@ export const putRequest = (url, payload, onSuccess, onError) => { ...@@ -141,16 +154,26 @@ export const putRequest = (url, payload, onSuccess, onError) => {
} }
export const postRequest = (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}`), { fetch((`${apiUrl}${url}`), {
method : 'POST', method : 'POST',
headers : fetchHeaders(), headers : newHeaders,
body: JSON.stringify(payload) body: payload instanceof FormData ? payload : JSON.stringify(payload)
}) })
.then(response => { .then(response => {
if (response.headers.has('access-token')) { if (response.headers.has('access-token')) {
updateAccessToken(response.headers.get('access-token')) updateAccessToken(response.headers.get('access-token'))
} }
return response.json() return response.json().catch(err => {
return {};
})
}) })
.then(data => { .then(data => {
console.log(data) console.log(data)
...@@ -172,7 +195,9 @@ export const fetchAllRequest = (urls, onSuccess, onError) => { ...@@ -172,7 +195,9 @@ export const fetchAllRequest = (urls, onSuccess, onError) => {
updateAccessToken(res.headers.get('access-token')) 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) => { }).then( (data) => {
onSuccess(data) onSuccess(data)
}).catch((error) => { }).catch((error) => {
...@@ -217,7 +242,9 @@ export const authentication = (url, payload, onSuccess, onError) => { ...@@ -217,7 +242,9 @@ export const authentication = (url, payload, onSuccess, onError) => {
sessionStorage.setItem('@portalmec/auth_headers', JSON.stringify(auth_headers)) sessionStorage.setItem('@portalmec/auth_headers', JSON.stringify(auth_headers))
return response.json() return response.json().catch(err => {
return {};
})
}) })
.then(data => { .then(data => {
console.log(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