Hi, guys! I am experiencing a very weird problem w...
# community-help
e
Hi, guys! I am experiencing a very weird problem where my typesense works when i use instantsearch to make the requests, but it fails with a lot of CORS related errors (even with cors disabled in my browser with a plugin) when i manually try to make the request.
import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter";
const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
server: {
apiKey: "xyz", // Be sure to use an API key that only allows search operations
nodes: [
{
host: "localhost",
port: "8108",
protocol: "http",
},
],
},
cacheSearchResultsForSeconds: 2 * 60, // Cache search results from server. Defaults to 2 minutes. Set to 0 to disable caching.
// The following parameters are directly passed to Typesense's search API endpoint.
//  So you can pass any parameters supported by the search endpoint below.
//  queryBy is required.
additionalSearchParameters: {
queryBy: "full_name",
},
});
const searchClient = typesenseInstantsearchAdapter.searchClient;
in this example, everything works perfectly in my app (even if i have to disable CORS in the browser)
import Typesense from 'typesense'
export default {
data() {
return {
queryString: '',
}
},
methods: {
search() {
const client = new Typesense.Client({
nodes: [
{
host: 'localhost',
port: '8108',
protocol: 'http',
},
],
apiKey: 'xyz',
connectionTimeoutSeconds: 2,
})
const searchParameters = {
q: 'Volvo',
query_by: 'name',
}
client.collections('brands').documents().search(searchParameters)
},
},
}
now in this example i get a lot of cors related errros in the console like: _OPTIONShttp://localhost:8108/collections/brands/documents/search?q=Volvo&query_by=nameCORS Preflight Did Not Succeed_ _Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8108/collections/brands/documents/search?q=Volvo&query_by=name. (Reason: CORS preflight response did not succeed). Status code: 404._ am i missing something?
j
@Edson Feimberg You want to add the flag
--enable-cors
when starting Typesense server
even with cors disabled in my browser with a plugin
Side note: I wasn't aware you could do this. Thought this was a browser built-in security feature
e
i wil try restarting the server with --enable-cors, but is there any reason instant search could successfully make the requests without CORS issues while in the second approach not?
by the way... using --enable-cors worked! Weird!
thank you so much, Jason!
👍 1
j
@Edson Feimberg The reason the first one worked is because in the instantsearch adapter, we use multi_search (which uses POST) and send the API key as a query param instead of a header. This makes it a "simple request" and does not trigger a CORS request. More info here: https://medium.com/@f2004392/cors-preflight-request-options-9d05b9248e5a
❤️ 1