Cross-origin request blocked - CORS header

43 views
Skip to first unread message

João Victor

unread,
Jun 25, 2020, 2:13:14 PM6/25/20
to ngrest
I have a web application developed with Vue.js and I would like to use the routes I developed with ngrest, but the requests
(POST's and GET's) are blocked, how can I add the header 'Access-Control-Allow-Origin': '*' at ngrest? I tried to use a filter,
but to no avail, maybe I'm doing it wrong.

Dmitry Utkin

unread,
Jun 25, 2020, 4:14:19 PM6/25/20
to ngrest
Using filter on post-dispatch phase is a way to go.. 

freitasjo...@gmail.com

unread,
Jun 25, 2020, 4:26:26 PM6/25/20
to ngrest
Hello Dmitry, first I would like to congratulate you for the excellent work and thank you for the immediate response!

One thing I didn't quite understand about filters (I was trying to use this to fix the problem): should I create a service, for example: "ngrest addservice myfilters" and do the treatment with that service? otherwise, where should I add the files with the logic of my filters?

loe...@gmail.com

unread,
Jun 26, 2020, 5:18:46 AM6/26/20
to ngrest
No, you should create a separate class inherited from FilterGroup, build it to the dynamic library and put it to the "filters" dir.

For the example please see filters test:
https://github.com/loentar/ngrest/tree/master/tests/filters

freitasjo...@gmail.com

unread,
Jun 26, 2020, 4:39:34 PM6/26/20
to ngrest
So, Dmitry, I tried several ways and when checking the filters at the URL http://localhost:9098/ngrest/operation/ngrest.ServerStatus/getFilters
there are no filters.

I added the sample files you sent me to the folders:

- .ngrest/ngrest-build/deploy/share/ngrest/filters/src
- .ngrest/ngrest/filters/src
- myproject/filters/src

None worked, the files are:

- CMakeLists.txt at the root of the filters folder
- src/TestFilterGroup.cpp
- src/TestFilterGroup.h

It probably isn't working because I didn't do this step: "build it to the dynamic library", how do I do it?

loe...@gmail.com

unread,
Jun 27, 2020, 5:04:28 AM6/27/20
to ngrest
Please see example attached

instructions:

unzip allowxoriginfilter.zip 
mkdir allowxoriginfilter-build && cd allowxoriginfilter-build
cmake ../allowxoriginfilter
make
mkdir -p ~/.ngrest/ngrest-build/deploy/share/ngrest/filters
cp deploy/share/ngrest/filters/allowxoriginfilter.so ~/.ngrest/ngrest-build/deploy/share/ngrest/filters

restart ngrest simple server.

Now the required header appears for every response:


allowxoriginfilter.zip
Reply all
Reply to author
Forward
0 new messages