Skip to content

Commit 41f9aaa

Browse files
kenfdevalexellis
authored andcommitted
update
1 parent 545dc51 commit 41f9aaa

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

docs/reference/cors.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,30 @@ Mainly there are two ways to deal with this problem. Either set a CORS header (w
2626

2727
This way, the request from the browser doesn't violate the **Same-origin policy** and under the hood, would be proxied to the OpenFaaS gateway.
2828

29-
## An Example with a Vue.js + Webpack Application
29+
## An Example with a Vue.js + Webpack Application
30+
31+
Let's look at a very minimal Vue.js Single Page application setup with webpack to actually see how it works. A developer will run `npm run dev` to spin up a local dev server via webpack. Suppose this was served on `http://localhost:8081`. By default, the OpenFaaS gateway will be exposed on `http://localhost:8080`. This means the local dev server and the OpenFaaS gateway have **different origins**. Therefore, if the Vue.js application requests data from the OpenFaaS gateway (`http://localhost:8080`), it will fail because it violates the **Same-origin policy**.
32+
33+
The code below is accessing an `echo` function from the Vue.js application:
34+
35+
```js
36+
// url to the echo function
37+
const url = 'http://localhost:8080/function/echo';
38+
39+
// sample payload
40+
const data = {
41+
test: 'test'
42+
};
43+
44+
// ajax request
45+
fetch(url, {
46+
body: JSON.stringify(data),
47+
method: 'POST'
48+
}).then(res => res.json())
49+
```
50+
51+
and ends up with the following error:
52+
53+
```
54+
Failed to load http://localhost:8080/function/echo: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
55+
```

0 commit comments

Comments
 (0)