How do I get the response as json to display in the console log of a url using axios?

Viewed 14

The code I am writing is attempting to console.log a random Wikipedia article URL using wikipedia's random article link. (https://en.wikipedia.org/wiki/Special:Random)

I am trying to use axios.get with the above URL and to return the response (which should include the randomly generated link) that the URL generates. (I would like to pinpoint on just console.logging this generated link and no other data, but that is the next step).

Here is the code I have written (in my app.html runner() is ran).

  function run(){
axios.get("https://en.wikipedia.org/wiki/Special:Random", {
  responseType: "json",
})
.then(function (response) {
  console.log(response.data);
});
}

function runner(){
  run()
}

I am getting these errors:

Access to XMLHttpRequest at 'https://en.m.wikipedia.org/wiki/Special:Random' (redirected from 'https://en.wikipedia.org/wiki/Special:Random') from origin '(it's classified)' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

,

 GET https://en.m.wikipedia.org/wiki/Special:Random net::ERR_FAILED 302
(anonymous) @ isAxiosError.js:12
e.exports @ isAxiosError.js:12
e.exports @ isAxiosError.js:12
l.request @ isAxiosError.js:12
r.forEach.l.<computed> @ isAxiosError.js:12
(anonymous) @ isAxiosError.js:12
run @ factbotprogram.js:20
runner @ factbotprogram.js:29
(anonymous) @ app.html:12

and

{
    "message": "Network Error",
    "name": "AxiosError",
    "config": {
        "transitional": {
            "silentJSONParsing": true,
            "forcedJSONParsing": true,
            "clarifyTimeoutError": false
        },
        "transformRequest": [
            null
        ],
        "transformResponse": [
            null
        ],
        "timeout": 0,
        "xsrfCookieName": "XSRF-TOKEN",
        "xsrfHeaderName": "X-XSRF-TOKEN",
        "maxContentLength": -1,
        "maxBodyLength": -1,
        "env": {
            "FormData": null
        },
        "headers": {
            "Accept": "application/json, text/plain, */*"
        },
        "responseType": "json",
        "method": "get",
        "url": "https://en.wikipedia.org/wiki/Special:Random"
    },
    "code": "ERR_NETWORK",
    "status": null
}

However, one thing I found: If I go to 'network' in dev tools there are 2 Special:Random Fetch/XHR entires, the second one is red. Only if I click on that red one and look in it's 'Response Headers' do I get the information I am looking for, assigned to variable 'location' (this is the URL of a random wiki article). How do I pinpoint this and log, or assign to variable, just that 'location' url string?

0 Answers
Related