-
-
Save alexcarpenter/5944528837f163b6fbd47a2cec87fea8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qs from 'qs'; | |
import Axios from 'axios'; | |
Axios.defaults.baseURL = Craft.baseUrl; | |
Axios.defaults.headers.common['Accept'] = 'application/json'; | |
Axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; | |
Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; | |
Axios.interceptors.request.use(config => { | |
if (config.data === Object(config.data)) { | |
config.data = qs.stringify(Object.assign({[Craft.csrfTokenName]: Craft.csrfToken}, config.data)); | |
} | |
return config; | |
}); | |
window.axios = Axios; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="x-ua-compatible" content="ie=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<title>Axios with Craft</title> | |
{% include '_includes/meta/index' %} | |
<link rel="stylesheet" href="{{ craft.config.baseUrl ~ 'assets/app.css' }}"> | |
</head> | |
<body> | |
<div id="app"> | |
{% block layout %} | |
{% endblock %} | |
</div> | |
{% set jsCraft = { | |
baseUrl: craft.config.baseUrl, | |
csrfTokenName: craft.config.csrfTokenName, | |
csrfToken: craft.request.getCsrfToken, | |
} %} | |
<script type="text/javascript"> | |
window.Craft = {{ jsCraft|json_encode|raw }}; | |
</script> | |
<script src="{{ craft.config.baseUrl ~ 'assets/app.js' }}"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment