问题
Since last couple of hours when I try to connect api.ai using javascript I get the following error .
"Failed to load https://api.api.ai/v1/query?v=20150910: No 'Access-Control-Allow-Origin' header is present on the requested resource."
I try to send request to Api.AI using javascript by following way:-
const client = new ApiAi.ApiAiClient({ accessToken: "*******************" }); const promise = client.textRequest(query); promise .then(handleResponse) .catch(handleError);
function handleResponse(serverResponse) {}
回答1:
You can use browser Extension for this problem. In chrome If you use PostMan or other service except browser you don't see this error or you can use nginx proxy to get result.
回答2:
I too have started facing this issue from few hours, it looks like people at DialogFlow have changed the CORS restrictions.
Right now, only solution is to use a middle proxy server
/ cloud function
which will call the api.ai url on behalf of browser and send the result back to browser.
Found the issue and answer here.
回答3:
I guess you are calling the API from the browser. It is not a great option here as anyone can see your access code.
You can try the following solutions:
- Call the API from the back-end - not front-end.
- Use fetch API and disable CORS.
来源:https://stackoverflow.com/questions/52789961/how-to-use-cors-in-javascript-to-get-response-from-dialogflow-api-ai