首页 > 解决方案 > 将 RASA POST webhook API 连接到 React Web UI 时出错

问题描述

这是用于连接 RASA webhook API 的反应服务器端代码。我的状态为 0。但在 RASA 框架中正常工作并为此请求发送答案。问题是我无法获取 API 调用的重新运行答案。

从“反应”导入反应,{组件}

export const rasaAPI = async function RASA(name, dialogue) {
    // POST request using fetch with error handling

    await fetch('http://192.168.8.100:5005/webhooks/rest/webhook', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
          'charset':'UTF-8'
        },
        body: JSON.stringify({ "sender": name, "message": dialogue }),
    })
    .then(function(response) {
        if(response.ok) {
          return response.blob();
      }
      throw new Error(response.status);
     })
     .then(response => response.json())
     .then(result => {
       console.log('Success:', result);
     })
     .catch(error => {
       console.error('Error:', error);
     });

}

这是 API 请求的控制台错误。

这是 API 请求的控制台错误。

标签: reactjsresthttp-postrasa

解决方案


正确答案 :

React 应用程序:从 'react' 导入 React,{ Component }

    export const rasaAPI = async function RASA(name, dialogue) {
        // POST request using fetch with error handling
        await fetch('/webhook', {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'charset':'UTF-8',
          },
          credentials: "same-origin",
          body: JSON.stringify({ "sender": name, "message": dialogue }),
      }).then(response => {
        return response.json();
      }).then(massage => {
        console.log(massage);
      });
    
    }

将 URL 添加到 package.json 文件中。

将 URL 添加到 package.json 文件中。


推荐阅读