I get this error:
ERROR Error: "Uncaught (in promise): Error Code: 0
Message: Http failure response for https://xxxxxxxxxx.com/xxxxx: 0 Unknown Error[object Object]"
I use this code to Ionic 4 project:
const headers = new HttpHeaders({ 'Content-Type': 'text/xml' });
return await this.http
.get("https://xxxxxxxxxx.com/xxxxx", { headers: httpHeaders })
.toPromise();
The server https://xxxxxxxxxx.com is setting to : accept allow origin CORS *
In the browser, the request is 200 ok and there is the data expected
Related
I'm trying to GET some data from our business server using a GET request with Vue and Axios. I encounter a 401 unauthorized error however. I'm able to GET this data with RESTED when I log in with my company account. I've already looked at this post: How to send Basic Auth with axios but no solution worked for me. This is my code to make the get request:
await axios.get('http://192.168.*******', {}, {
auth: {
username: 'username',
password: 'password'
},
headers: {
'Content-Type': 'application/json'
}
});
}
I've also tried without the 'headers'. This is the error message:
xhr.js:214 GET http://192.******** 401 (Unauthorized)
dispatchXhrRequest # xhr.js:214
[Vue warn]: Unhandled error during execution of created hook
at <App>
createError.js:19 Uncaught (in promise) Error: Request failed with status code 401
at createError (createError.js:19:15)
at settle (settle.js:19:12)
at XMLHttpRequest.onloadend (xhr.js:75:7)
Hopefully someone has an idea because I'm at a loss.
This solution has worked for me:
const config = {
'Content-Type': 'application/json',
"Accept": "application/json",
}
const { data } = await axios({ url: 'http://192.168.170.*****/', method: 'GET', data: null, withCredentials: true, headers: config })
I'm trying to get some simple data from an api via axios and vuejs.
axios.get('https://www.coinspot.com.au/pubapi/v2/latest')
.then((response) => {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
});
I get the following error:
GET http://localhost:8080/pubapi/v2/latest 404 (Not Found)
Uncaught (in promise) Error: Request failed with status code 404 at createError (createError.js?2d83:16) at settle (settle.js?467f:17) at XMLHttpRequest.handleLoad (xhr.js?b50d:62)
I can get this data to return in postman without anything special. How do I get this data to return without cors or 404 errors in my vuejs app?
try using this
axios({
method: 'get',
url: 'https://www.coinspot.com.au/pubapi/v2/latest',
}).then((response) => {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
});
I'm sending GET request to the same endpoint with the same headers, including the Bearer, but while i get 200 and the correct JSON as a result when calling from Postman, when i send the request using Axios in my (Vue) project i get 302.
the call from the local project , running on localhost:8080 (in case its useful) is as follows:
const { authToken, userID } = store.getters.authUser
const config = {
method: 'get',
url: '/' + userID,
headers: {
Authorization: `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
}
axios(config)
.then(res => {
console.log(res)
return res
})
.catch(e => {
console.log(e)
})
while in postman all i do is create a GET request with the same url and all i add in the headers is the 'Bearer ...'
the error i get from axios is :
Error: Network Error
at createError (createError.js?2d83:16)
at XMLHttpRequest.handleError (xhr.js?b50d:87)
and response status is 302 , any idea whats going on here??
both values of userID and the authToken exists and are equal to the same values i used with postman, same goes for the url
In my SPA made in VUE I'm setting my Axios repository (or Api file) but I'm getting trouble using interceptors.
Basically, if the jwtoken is expired or missing the interceptor works and I see in the debug the 401 error and I see in network debug the attempt to the api server.
The issue is when I got a valid jwtoken. In this case I have no attempt in network window and the error:
TypeError: Cannot read property 'toUpperCase' of undefined
at dispatchXhrRequest (app.js:57825)
at new Promise (<anonymous>)
at xhrAdapter (app.js:57808)
at dispatchRequest (app.js:58416)
This should mean a promise error and maybe a config error, but I need fresh eyes to fix...
This is the request code having promise:
const headers = {
'X-CSRF-TOKEN': csrfToken,
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
};
client.interceptors.request.use(
config => {
if (!token) {
return config;
}
const newConfig = {
baseURL,
withCredentials: false,
headers: headers
};
return newConfig;
},
e => Promise.reject(e)
);
The stack trace suggests the problem is in dispatchXhrRequest. There's a call to toUpperCase here:
https://github.com/axios/axios/blob/2ee3b482456cd2a09ccbd3a4b0c20f3d0c5a5644/lib/adapters/xhr.js#L28
So it looks like you're missing config.method.
My guess would be that you need to copy the old config into your new config so you get all the other options, such as method:
const newConfig = {
...config,
baseURL,
withCredentials: false,
headers: headers
};
I am using axios get request with headers in vuejs application. Working fine and getting response in chrome and firefox. But in edge its not working, getting Network Error. I am sending the request with header Authorization.
axios.get(url, {
headers: {
'Authorization': 'Basic bmlwerydmFdfgdfgdfllobjhJayw='
}
})
.then(function(response) {
console.log("Success");
}
}).catch(function(error) {
console.log(error);
});
showing following error in console.
[object Error]: {config: Object, description: "Network Error", message:
"Network Error", request: Object, response: undefined...}
config: Object
description: "Network Error"
message: "Network Error"
request: Object
response: undefined
stack: "Error: Network Error at e.exports
(https://unpkg.com/axios#0.18.0/dist/axios.min.js:8:4473) at l.onerror
(https://unpkg.com/axios#0.18.0/dist/axios.min.js:8:3317)"
__proto__: Object
if we send get request without any header it will work fine.How can i call axios with header Authorization.
Thank you