Created
January 31, 2018 08:49
-
-
Save danigm/f7e50543d99278dad7255f299fe2cae6 to your computer and use it in GitHub Desktop.
hug cors example
This file contains 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 hug | |
from hug.middleware import CORSMiddleware | |
api = hug.API(__name__) | |
api.http.add_middleware(CORSMiddleware(api)) | |
@hug.post('/demo') | |
def demo(name: 'your name'): | |
return {"result": "Hello {0}".format(name)} | |
This file contains 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
<html> | |
<body> | |
<script> | |
let url = 'http://localhost:8000/demo'; | |
let data = {'name': 'danigm'}; | |
fetch(url, { | |
method: 'POST', | |
body: JSON.stringify(data), | |
headers: new Headers({ | |
'Content-Type': 'application/json' | |
}) | |
}) | |
.then(resp => resp.json()) | |
.then(js => console.log(js)); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment