Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  82] [ 1]  / answers: 1 / hits: 18762  / 5 Years ago, mon, june 24, 2019, 12:00:00

I want to test my app and get data from api. When I'm logged in to the web I check in the network tab, which returns me request url in headers, example: https: // beta.application.com / api / v1 / projects /. Is it possible for me to refer to this url in my app?



In console.log I have error




Failed to load resource: the server responded with a status of 401
(Unauthorized)



Access to https: // beta.application.com / api / v1 / projects / at from origin 'chrome-extension://ccekbkp' has
been blocked by CORS policy: Response to preflight request doesn't
pass access control check: The value of the
'Access-Control-Allow-Origin' header in the response must not be the
wildcard '*' when the request's credentials mode is 'include'. The
credentials mode of requests initiated by the XMLHttpRequest is
controlled by the withCredentials attribute.



Network Error
at createError (webpack-internal:///./node_modules/axios/lib/core/createError.js:16)
at XMLHttpRequest.handleError (webpack-internal:///./node_modules/axios/lib/adapters/xhr.js:87)




Example:




email: [email protected]



password: aZdfgHkL12




In local.storage I have access to token
Example: `{access_token: 111111111111111111 }



Can I copy this token and use it in my app in React?



I'm trying to do it:



  componentDidMount() {
const token = '111111111111111111'; //token from local.storage
/*const hash = Base64.encode(token);
const Basic = 'Basic ' + hash;*/
const username= '[email protected]';
const password= 'aZdfgHkL12'
const url= https://beta.application.com/api/v1/projects/;

axios.get
axios({
url: url,
withCredentials: true,
method: GET,
headers: {
'Authorization': `Bearer ${token}`
},
auth: {
username: username,
password: password
}
})
.then(response => {
this.setState({
projects: response
});
})
.catch(error => {
console.log('error');
})
}

More From » reactjs

 Answers
9

Solution:



componentDidMount() {
const token = '111111111111111111'; //token from local.storage
const url= https://beta.application.com/api/v1/projects;

axios.get
axios({
url: url,
method: GET,
headers: {
'Authorization': `Bearer ${token}`
}
})
.then(response => {
this.setState({
projects: response
});
})
.catch(error => {
console.log('error');
})
}

[#51964] Monday, June 17, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kayden

Total Points: 546
Total Questions: 102
Total Answers: 95

Location: Virgin Islands (U.S.)
Member since Fri, Mar 4, 2022
2 Years ago
;