Where can I find JSON data?
See my answer below (this one was wrong).
Change your server function to this and it works :
@anvil.server.http_endpoint('/list-people')
def list_people():
return [{'name': Name['Name']} for Name in app_tables.names.search()]
For example, my local version :
https://y4xhys5qb3h4eo2b.anvil.app/_/private_api/5KPGNIMD6CHQGL5DY3ISVQGR/list-people
Shows this :
0 | |
---|---|
name | “Adam” |
1 | |
name | “Sam” |
Just as an experiment, try using this URL :
Names is the table name (the main object) and Name column is the attribute of the Table Names.
Do you know what the error is in your client?
Could you be suffering from a CORS issue?
Not sure, maybe; I am not writing JSON format correctly at the client side!
Ok, change your function to be this :
@anvil.server.http_endpoint('/list-people')
def list_people():
r = anvil.server.HttpResponse()
r.headers['Access-Control-Allow-Origin'] = '*'
r.headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
r.headers['Access-Control-Request-Method'] = '*'
r.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
r.status = 200
r.body = [{'name': Name['Name']} for Name in app_tables.names.search()]
return r
I will try it and let you know.
I use this website to test all my API stuff :
I just copied the url into the “Endpoint” textbox and clicked the “Ajax Request” button.
My URL is :
https://thoughtful-opulent-raw.anvil.app/_/api/list-people
I will try it and get back to you, thank you so much!
Thank you, it worked!
Do you know where to find a documentation/tutorial to explain how to write JSON at server side?
Glad it working for you.
Are you looking for general Python JSON docs? If so : https://realpython.com/python-json/