Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable CORS access from JavaScript on other domains #24

Closed
simonw opened this issue Aug 6, 2020 · 3 comments
Closed

Enable CORS access from JavaScript on other domains #24

simonw opened this issue Aug 6, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Aug 6, 2020

This requires extra CORS headers to allow the content-type header and the POST method.

@simonw simonw added the enhancement New feature or request label Aug 6, 2020
@simonw
Copy link
Owner Author

simonw commented Aug 6, 2020

I'm going to enable CORS by default - if you want to disable it you can use a plugin setting.

@simonw
Copy link
Owner Author

simonw commented Aug 6, 2020

Some test HTML - pasted into http://htmledit.squarefree.com/ (has to be an HTTP site because HTTPS cannot make CORS requests to localhost):

<textarea></textarea>
<script>
fetch('http://127.0.0.1:8009/graphql/til', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ query: '{ til { totalCount } }' }),
})
  .then(res => res.json())
  .then(res => document.getElementsByTagName('textarea')[0].value = JSON.stringify(res.data));
</script>

@simonw simonw closed this as completed in bbd5389 Aug 6, 2020
@simonw
Copy link
Owner Author

simonw commented Aug 6, 2020

This works! Demo: https://iridescent-neat-wolfberry.glitch.me/

fetch('https://datasette-graphql-demo.datasette.io/graphql', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ query: '{ repos { totalCount } }' }),
})
  .then(res => res.json())
  .then(res => document.getElementById('num').innerHTML = res.data.repos.totalCount);

simonw added a commit that referenced this issue Aug 6, 2020
simonw added a commit to simonw/covid-19-datasette that referenced this issue Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant