Hello. I've imported axios npm module in my Wix site and I've imported it in the code.
And I have a simple axiosGET() function which it tries to get data from any given URL whether it's a REST API URL responding with JSON Object or a web site URL giving HTML content.
import axios from 'axios';
class Helper {
async axiosGET(url, config) {
console.log("axiosGET(): url="+ url);
try {
const resp = await axios.get(url, config);
console.log("axiosGET(): resp.keys=" + JSON.stringify(Object.keys(resp)));
} catch (err) {
console.log("axiosGET(): catch: err=" + JSON.stringify(err));
}
}
}
And I call this function from another async function like below:
const result = await helper.axiosGET("https://jsonplaceholder.typicode.com/todos/1", null);
OR
const result = await helper.axiosGET("https://www.google.com/", null);
The first URL responds with a JSON Object. And this works fine.
But the second URL or any URL that returns HTML content ends in an error and I don't understand why??? I tried to include headers like "Accept": "*/*" or "text/html" but that didn't help either.
This is what I get on the first URL which shows that it worked fine:
axiosGET(): url=https://jsonplaceholder.typicode.com/todos/1
axiosGET(): resp.keys=["data","status","statusText","headers","config","request"]
And this is what I get on the second URL or any URL which gives HTML content:
axiosGET(): url=https://www.google.com/
axiosGET(): catch: err={"message":"Network Error","name":"AxiosError","stack":"AxiosError@https://c18093e5-800c-4582-a788-48b9a3a8e2e4.dev.wix-code.com/pages/kie9n.js?viewMode=preview&instance=wixcode-dev.6b78a375c600b59ae072b235378a02b379023e91.eyJpbnN0YW5jZUlkIjoiYzE4MDkzZTUtODAwYy00NTgyLWE3ODgtNDhiOWEzYThlMmU0IiwiY29ycmVsYXRpb25JZCI6ImJhZmE2MGI0LTBmYmItNGJiYy04YzgzLTQ5NzU3M2QxMmZmNiIsInNlc3Npb25VSWQiOiIwMmYwMzNhNy0xZmU0LTRkMWMtYTFjYy05NWI4MjExMTMxNWIiLCJpc1RlbXBsYXRlIjpmYWxzZSwic2lnbkRhdGUiOjE2NzI5NjM0OTM1MTYsInNpdGVNZW1iZXJJZCI6IjAyZjAzM2E3LTFmZTQtNGQxYy1hMWNjLTk1YjgyMTExMzE1YiIsInVpZCI6IjAyZjAzM2E3LTFmZTQtNGQxYy1hMWNjLTk1YjgyMTExMzE1YiIsImFwcERlZklkIjoiQ2xvdWRTaXRlRXh0ZW5zaW9uIiwiaXNBZG1pbiI6dHJ1ZSwibWV0YVNpdGVJZCI6IjczZTNlODU2LWVmZTYtNGMxMi05MDI1LWI4MGFiNGNlMWMyMCIsImNhY2hlIjpudWxsLCJleHBpcmF0aW9uRGF0ZSI6IjIwMjMtMDEtMDZUMDQ6MDQ6NTMuNTE2WiIsInNpdGVPd25lcklkIjoiMDJmMDMzYTctMWZlNC00ZDFjLWExY2MtOTViODIxMTEzMTViIiwiaW5zdGFuY2VUeXBlIjoiZGV2IiwicGVybWlzc2lvblNjb3BlIjpudWxsfQ==&gridAppId=f7ce043e-f5e0-4a2c-8057-19c1c8e0a202&analyze-imported-namespaces=false&init-platform-api-provider=false&get-app-def-id-from-package-name=false&inject-elementory-support=true&dependencies-token=3938:34097:19\nhandleError@https://c18093e5-800c-4582-a788-48b9a3a8e2e4.dev.wix-code.com/pages/kie9n.js?viewMode=preview&instance=wixcode-dev.6b78a375c600b59ae072b235378a02b379023e91.eyJpbnN0YW5jZUlkIjoiYzE4MDkzZTUtODAwYy00NTgyLWE3ODgtNDhiOWEzYThlMmU0IiwiY29ycmVsYXRpb25JZCI6ImJhZmE2MGI0LTBmYmItNGJiYy04YzgzLTQ5NzU3M2QxMmZmNiIsInNlc3Npb25VSWQiOiIwMmYwMzNhNy0xZmU0LTRkMWMtYTFjYy05NWI4MjExMTMxNWIiLCJpc1RlbXBsYXRlIjpmYWxzZSwic2lnbkRhdGUiOjE2NzI5NjM0OTM1MTYsInNpdGVNZW1iZXJJZCI6IjAyZjAzM2E3LTFmZTQtNGQxYy1hMWNjLTk1YjgyMTExMzE1YiIsInVpZCI6IjAyZjAzM2E3LTFmZTQtNGQxYy1hMWNjLTk1YjgyMTExMzE1YiIsImFwcERlZklkIjoiQ2xvdWRTaXRlRXh0ZW5zaW9uIiwiaXNBZG1pbiI6dHJ1ZSwibWV0YVNpdGVJZCI6IjczZTNlODU2LWVmZTYtNGMxMi05MDI1LWI4MGFiNGNlMWMyMCIsImNhY2hlIjpudWxsLCJleHBpcmF0aW9uRGF0ZSI6IjIwMjMtMDEtMDZUMDQ6MDQ6NTMuNTE2WiIsInNpdGVPd25lcklkIjoiMDJmMDMzYTctMWZlNC00ZDFjLWExY2MtOTViODIxMTEzMTViIiwiaW5zdGFuY2VUeXBlIjoiZGV2IiwicGVybWlzc2lvblNjb3BlIjpudWxsfQ==&gridAppId=f7ce043e-f5e0-4a2c-8057-19c1c8e0a202&analyze-imported-namespaces=false&init-platform-api-provider=false&get-app-def-id-from-package-name=false&inject-elementory-support=true&dependencies-token=3938:35807:14\n","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"adapter":["xhr","http"],"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"env":{},"headers":{"Accept":"application/json, text/plain, */*"},"method":"get","url":"https://www.google.com/"},"code":"ERR_NETWORK","status":null}
Any thoughts?
I just tested this and it works as expected. Here is my code:
import axios from 'axios'; export async function test() { try { const response = await axios.get('https://www.google.com/'); console.log(response); } catch (error) { console.error(error); } }