How to use CORS in javascript to get response from Dialogflow (Api.Ai)

ぃ、小莉子 提交于 2019-12-10 10:47:23

问题


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:

  1. Call the API from the back-end - not front-end.
  2. Use fetch API and disable CORS.


来源:https://stackoverflow.com/questions/52789961/how-to-use-cors-in-javascript-to-get-response-from-dialogflow-api-ai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!