Skip to content

Commit

Permalink
Add CORS headers to servers
Browse files Browse the repository at this point in the history
  • Loading branch information
zpao committed Jan 26, 2016
1 parent 304a251 commit 7b675c8
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ func handleComments(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Access-Control-Allow-Origin", "*")
io.Copy(w, bytes.NewReader(commentData))

case "GET":
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Access-Control-Allow-Origin", "*")
// stream the contents of the file to the response
io.Copy(w, bytes.NewReader(commentData))

Expand Down
1 change: 1 addition & 0 deletions server.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function routeRequest()
}
header('Content-Type: application/json');
header('Cache-Control: no-cache');
header('Access-Control-Allow-Origin: *');
echo $comments;
} else {
return false;
Expand Down
1 change: 1 addition & 0 deletions server.pl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
my $self = shift;
my $comments = decode_json (do { local(@ARGV,$/) = 'comments.json';<> });
$self->res->headers->cache_control('no-cache');
$self->res->headers->access_control_allow_origin('*');

if ($self->req->method eq 'POST')
{
Expand Down
2 changes: 1 addition & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def comments_handler():
with open('comments.json', 'w') as file:
file.write(json.dumps(comments, indent=4, separators=(',', ': ')))

return Response(json.dumps(comments), mimetype='application/json', headers={'Cache-Control': 'no-cache'})
return Response(json.dumps(comments), mimetype='application/json', headers={'Cache-Control': 'no-cache', 'Access-Control-Allow-Origin': '*'})

if __name__ == '__main__':
app.run(port=int(os.environ.get("PORT",3000)))
1 change: 1 addition & 0 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
# always return json
res['Content-Type'] = 'application/json'
res['Cache-Control'] = 'no-cache'
res['Access-Control-Allow-Origin'] = '*'
res.body = JSON.generate(comments)
end

Expand Down

0 comments on commit 7b675c8

Please sign in to comment.