如何在JavaScript中使用CORS来获取Dialogflow(Api.Ai)的响应 [英] How to use CORS in javascript to get response from Dialogflow (Api.Ai)

查看:97
本文介绍了如何在JavaScript中使用CORS来获取Dialogflow(Api.Ai)的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自上几个小时尝试使用javascript连接api.ai以来,出现以下错误.

Since last couple of hours when I try to connect api.ai using javascript I get the following error .

无法加载 https://api.api.ai/v1/query ?v = 20150910 :所请求的资源上没有"Access-Control-Allow-Origin"标头."

"Failed to load https://api.api.ai/v1/query?v=20150910: No 'Access-Control-Allow-Origin' header is present on the requested resource."

我尝试通过以下方式使用javascript将请求发送到Api.AI:-

I try to send request to Api.AI using javascript by following way:-

const client = new ApiAi.ApiAiClient({ accessToken:"*******************" }); const promise = client.textRequest(query); 承诺 .then(handleResponse) .catch(handleError);

const client = new ApiAi.ApiAiClient({ accessToken: "*******************" }); const promise = client.textRequest(query); promise .then(handleResponse) .catch(handleError);

function handleResponse(serverResponse){}

function handleResponse(serverResponse) {}

推荐答案

我也已经从几个小时开始面对这个问题,看来DialogFlow的人们已经改变了CORS限制.

I too have started facing this issue from few hours, it looks like people at DialogFlow have changed the CORS restrictions.

现在,唯一的解决方案是使用中间的proxy server/cloud function,它将代表浏览器调用api.ai URL并将结果发送回浏览器.

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.

发现了问题并在此处回答.

这篇关于如何在JavaScript中使用CORS来获取Dialogflow(Api.Ai)的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆