Peter M.
Peter M.

Reputation: 856

Fetching JSON data with PyScript

I want to fetch json data with PyScript but cannot get over a

pyodide.ffi.JsException: TypeError: NetworkError when attempting to fetch resource.

It works with the sample data (the outcommented lines) but not with my json data which looks valid to me:

[
    {
      "name": "Maximilian Bauer",
      "birthdate": "15. Maerz 1985"
    },
    {
      "name": "Sophie Schneider",
      "birthdate": "22. Juli 1990"
    }
]
from pyscript import fetch

# response = await fetch(
#    "https://microsoftedge.github.io/Demos/json-dummy-data/64KB.json",
#    method="GET"
#).json()

response = await fetch(
    "https://www.nanobooks23.de/material/schachclub2.json",
    method="GET"
).json()

print(response)

And do I have to use fetch() from the js module or the pyscript module or doesn't it matter?

Update: The json file is served by a web server that probably does not use the "CORS-Header":

Access-Control-Allow-Origin: *

So I would have to use a "CORS-Enabled API"?

Upvotes: 0

Views: 43

Answers (1)

Peter M.
Peter M.

Reputation: 856

Its CORS of course, when I use a CORS proxy like

https://corsproxy.io/?

and put it before the url like

https://corsproxy.io/?https://www.nanobooks23.de/material/schachclub2.json

it works.

Upvotes: 0

Related Questions