Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable CORS for Catalyst

Having a Perl Catalyst application, which produces JSON, I need to read that JSON content using jQuery within an HTML page, served by an Apache server. Both applications, Catalyst and Apache are running on the same host.

When I access the Catalyst URL from Apache I get the error

Access to XMLHttpRequest at 'http://localhost:3000/abc/json_list' from origin 'http://localhost:8888' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

As I red in many topics, a header (or more) must be set. In this case the Catalyst must be set but I don't know how.

Any hint?

like image 997
Kérdezösködő Indián Avatar asked Dec 30 '25 05:12

Kérdezösködő Indián


1 Answers

Catalyst allows you to set response headers using the header method on the response object.

$c->res->header( "Access-Control-Allow-Origin" => "http://localhost:8888" );

Consider using a controller's sub auto or using existing middleware if you have multiple endpoints that need to provide permission via CORS.

like image 180
Quentin Avatar answered Dec 31 '25 18:12

Quentin