Skip to content

Commit 7b675c8

Browse files
committed
Add CORS headers to servers
1 parent 304a251 commit 7b675c8

File tree

5 files changed

+6
-1
lines changed

5 files changed

+6
-1
lines changed

server.go

+2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ func handleComments(w http.ResponseWriter, r *http.Request) {
8484

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

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

server.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function routeRequest()
4545
}
4646
header('Content-Type: application/json');
4747
header('Cache-Control: no-cache');
48+
header('Access-Control-Allow-Origin: *');
4849
echo $comments;
4950
} else {
5051
return false;

server.pl

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
my $self = shift;
2121
my $comments = decode_json (do { local(@ARGV,$/) = 'comments.json';<> });
2222
$self->res->headers->cache_control('no-cache');
23+
$self->res->headers->access_control_allow_origin('*');
2324

2425
if ($self->req->method eq 'POST')
2526
{

server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def comments_handler():
3030
with open('comments.json', 'w') as file:
3131
file.write(json.dumps(comments, indent=4, separators=(',', ': ')))
3232

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

3535
if __name__ == '__main__':
3636
app.run(port=int(os.environ.get("PORT",3000)))

server.rb

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
# always return json
4141
res['Content-Type'] = 'application/json'
4242
res['Cache-Control'] = 'no-cache'
43+
res['Access-Control-Allow-Origin'] = '*'
4344
res.body = JSON.generate(comments)
4445
end
4546

0 commit comments

Comments
 (0)