Skip to content

Commit

Permalink
fix cors options error
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Apr 11, 2019
1 parent 064dc3c commit fa7541d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions examples/cors-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>

<body>
<h1>Hello CORS</h1>
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<script>
console.log("Hello")
$.ajax({
url: "http://localhost:4000/api/v1/user/devices/6EB0217704000486",
method: "get",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Bearer 3460b7a59def4bfa926a9b643504b8b7")
},
}).then(ret => {
console.log(ret)
})
</script>
</body>

</html>
4 changes: 3 additions & 1 deletion web/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class AuthRequestHandler(BaseRequestHandler):
""" request user logged in before http request """

async def prepare(self):
if self.request.method == "OPTIONS": # CORS
return
await super().prepare()
authenticated(lambda x: None)(self)

Expand Down Expand Up @@ -151,7 +153,7 @@ def set_default_headers(self):
self.set_header("Access-Control-Allow-Headers", self.CORS_HEADERS)
self.set_header('Access-Control-Allow-Methods', self.CORS_METHODS)

def options(self):
def options(self, *args):
# no body
self.set_status(204)
self.finish()
Expand Down

0 comments on commit fa7541d

Please sign in to comment.