# Can use axios to get external data?

**URL:** <https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364>\
**Category:** Help\
**Created:** [October 29, 2018, 3:36am UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364 "2018-10-29T03:36:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![goldmine](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/goldmine/32/1191_2.png) [@goldmine](https://talk.observablehq.com/u/goldmine)\
**Post date:** [October 29, 2018, 3:36am UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364/1 "2018-10-29T03:36:25Z")

</div>

got network error always. anyone can help? thanks!

---

<div class="post-metadata">

**Author:** ![bgchen](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bgchen/32/784_2.png) [@bgchen](https://talk.observablehq.com/u/bgchen)\
**Post date:** [October 29, 2018, 3:49am UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364/2 "2018-10-29T03:49:17Z")

</div>

Could you link to an example notebook that illustrates the issue?

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [October 29, 2018, 3:52am UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364/3 "2018-10-29T03:52:32Z")

</div>

As I understand it, there’s less of a reason to use Axios now that the [Fetch standard](https://fetch.spec.whatwg.org) is supported by modern browsers. Perhaps you are running into CORS issues? Have you read the introduction to data notebook?

> **[Introduction to Data](https://beta.observablehq.com/@mbostock/introduction-to-data)**
>
> An Observable notebook by Mike Bostock.

---

<div class="post-metadata">

**Author:** ![goldmine](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/goldmine/32/1191_2.png) [@goldmine](https://talk.observablehq.com/u/goldmine)\
**Post date:** [October 29, 2018, 7:07am UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364/4 "2018-10-29T07:07:58Z")

</div>

I need data from remote js file, like this “[http://lol.qq.com/biz/hero/champion.js](http://lol.qq.com/biz/hero/champion.js)”, I want to load it directly instead of saving it to local. Sorry, I am a new developer, maybe there is better way to implement.

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [October 29, 2018, 3:08pm UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364/5 "2018-10-29T15:08:08Z")

</div>

Sounds like you need a CORS proxy. You can’t fetch directly from an HTTP site (HTTPS is required) and the server must have [CORS enabled](https://enable-cors.org). You can try the demo instance of CORS-Anywhere to start.

> **[Rob--W/cors-anywhere](https://github.com/Rob--W/cors-anywhere)**
>
> CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request. - Rob--W/cors-anywhere

---

<div class="post-metadata">

**Author:** ![bgchen](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bgchen/32/784_2.png) [@bgchen](https://talk.observablehq.com/u/bgchen)\
**Post date:** [October 31, 2018, 1:48am UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364/6 "2018-10-31T01:48:01Z")

</div>

For what it’s worth, I tried to use a few different CORS proxies to load your data, but without success:

> **[Loading data from external JS file / Observable](https://beta.observablehq.com/d/16c17d9e954eada7)**
>
> A better way to code.

I couldn’t get CORS-anywhere to work at all. The other two proxies send the JS file with the wrong MIME Content-type: [cors.io](http://cors.io) sends it as `text/html` and codetabs sends it as `text/plain`.

---

<div class="post-metadata">

**Author:** ![tom](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tom/32/2262_2.png) [@tom](https://talk.observablehq.com/u/tom)\
**Post date:** [October 31, 2018, 3:56am UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364/7 "2018-10-31T03:56:38Z")

</div>

This might be a bit confusing at first because the file isn’t data - it’s JavaScript code. So solving CORS, using axios, and other steps aren’t directionally right. You can, however, achieve what you want to do:

```
require("https://lol.qq.com/biz/hero/champion.js").catch(() => window.LOLherojs)

```

This uses `require()`, our method for including code, but since `champion.js` doesn’t implement UMD or any sort of ‘module definition’ standard and instead just leaves a variable behind, we catch that variable (what I call the ‘global leaks pattern’ in the module debugger).

(The other minor quirk is that I changed the URL from `http://` to `https://`. [qq.com](http://qq.com) supports both, but http:// won’t work with Observable or any SSL website).

---

<div class="post-metadata">

**Author:** ![goldmine](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/goldmine/32/1191_2.png) [@goldmine](https://talk.observablehq.com/u/goldmine)\
**Post date:** [November 2, 2018, 1:08pm UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364/8 "2018-11-02T13:08:18Z")

</div>

Thanks for your time!

---

<div class="post-metadata">

**Author:** ![goldmine](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/goldmine/32/1191_2.png) [@goldmine](https://talk.observablehq.com/u/goldmine)\
**Post date:** [November 2, 2018, 1:08pm UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364/9 "2018-11-02T13:08:49Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![goldmine](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/goldmine/32/1191_2.png) [@goldmine](https://talk.observablehq.com/u/goldmine)\
**Post date:** [November 2, 2018, 1:10pm UTC](https://talk.observablehq.com/t/can-use-axios-to-get-external-data/1364/10 "2018-11-02T13:10:46Z")

</div>

It works!!! Really appreciate!
