using fetch() #2176
-
Hi everyone, I would like to use an online API than return a JSON.. out of topicI rather preferred had a YAML reply.. but it's not provided yet..The requests to the API work with a GET to an URL with I would like to process the data in the frontend. The code readability on the example is indecipherable, not the fault of MDN but because of the JS nature syntax , And no matter what, I can't put my head around it.. So how could we use Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I've tried this def f_succeed():
print('succeed')
window.fetch(window.encodeURI('https://theAPI.com?test=test'), {'redirect':'error'}).then(f_succeed) but of course it's not working... 😢 error
|
Beta Was this translation helpful? Give feedback.
-
Requests sent from a browser can only reach resources with the same origin as the calling script, except if the server where the resource stands sets the cross-origin headers. Is it the case in your example ? If so, you can use the Javascript-like syntax from browser import window, aio
async def f():
response = await window.fetch('test.html')
if response.status == 200:
text = await response.text()
print(text)
aio.run(f()) |
Beta Was this translation helpful? Give feedback.
-
As the documentation of fetch() says, it resolves to a def handle_content(s):
print(s)
def fetched(ResJSobj):
if ResJSobj.ok:
ResJSobj.text().then(handle_content)
else:
print("it didn't work")
window.fetch('/aLocalPage').then(fetched) |
Beta Was this translation helpful? Give feedback.
As the documentation of fetch() says, it resolves to a
Response
object, andResponse.text()
is a promise that resolves to the text content of the resource. You should do something like