Issue making cross origin requests #33
Replies: 2 comments
-
@DoctaCloak Currently, a temporary solution is to add headers related to cross-domain in mock config: {
"access-control-allow-credentials": "true",
"access-control-allow-origin": "*",
"access-control-allow-headers": "X-Requested-With,Content-Type",
"access-control-allow-methods": "*"
} The logic can be reused in more configurations through createDefineMock provided by plugins: // mock/shared.ts
import { createDefineMock } from 'vite-plugin-mock-dev-server'
export const defineCorsMock = createDefineMock((mock) => {
const headers = mock.headers || {}
mock.headers = {
'access-control-allow-credentials': 'true',
'access-control-allow-origin': '*',
'access-control-allow-headers': 'X-Requested-With,Content-Type',
'access-control-allow-methods': '*',
...headers,
}
return mock
}) // mock/api.mock.ts
import { defineCorsMock } from './shared'
export default defineCorsMock({
url: '/api/cors'
}) With the method of creating custom mock conversion function using |
Beta Was this translation helpful? Give feedback.
-
@DoctaCloak Starting from version |
Beta Was this translation helpful? Give feedback.
-
Hey I had a quick question, say I have an SPA running on port 8080 and I want to make a cross origin request to my vite plugin mock dev server which is running on port 9001, how would I go about doing so without getting a CORS error?
Normally when you want to do this with a node server you invoke
cors()
but that doesn't seem to be an option in my case and I can't get access to your express app to useapp.use(cors())
I have the following API endpoint that when calling it directly via POSTMAN works just fine but when making this GET request on the SPA that's on a different origin that doesn't seem to work.
Would appreciate any help on this 🙂
Beta Was this translation helpful? Give feedback.
All reactions