简体   繁体   English

烧瓶缺少XMLHttpRequest GET的输出

[英]Missing output for XMLHttpRequest GET with flask

I am getting started with chabots UI and REST APIs. 我开始使用Chabots UI和REST API。

I started by making a simple api with flask: 我首先用flask创建了一个简单的api:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

When I access http://EC2_HOSTNAME:5000 I can see Hello World! 当我访问http:// EC2_HOSTNAME:5000时,我可以看到Hello World!

Then, I adapted the example from https://github.com/botui/botui-examples/tree/master/git-stars-bot to simply return Hello World! 然后,我从https://github.com/botui/botui-examples/tree/master/git-stars-bot改编了示例,以简单地返回Hello World! whatever the user input is. 无论用户输入什么。

Here is my adapted stars-bot.js script: 这是我改编的stars-bot.js脚本:

var loadingMsgIndex,
    botui = new BotUI('stars-bot'),
    API = 'http://EC2_HOSTNAME:5000';

function sendXHR(repo, cb) {
  var xhr = new XMLHttpRequest();
  var self = this;
  xhr.open('GET', API);
  xhr.onload = function () {
    var res = xhr.responseText
    cb(res);
  }
  xhr.send();
}

function init() {
  botui.message
  .bot({
    delay: 1000,
    content: 'Enter the repo name to see how many stars it have:'
  })
  .then(function () {
    return botui.action.text({
      delay: 1000,
      action: {
        value: 'dummy user input',
        placeholder: 'user input'
      }
    })
  }).then(function (res) {
    loadingMsgIndex = botui.message.bot({
      delay: 200,
      loading: true
    }).then(function (index) {
      loadingMsgIndex = index;
      sendXHR(res.value, showStars)
    });
  });
}

function showStars(stars) {
  botui.message
  .update(loadingMsgIndex, {
    content: stars
  })
  .then(init); // ask again for repo. Keep in loop.
}

init();

However, when I access the index.html file and enter an input, the answer seems to be blank. 但是,当我访问index.html文件并输入一个输入时,答案似乎是空白。 I was expecting to get the Hello World! 我期待获得《 Hello World! value. 值。 By checking the flask logs, I also see that the status of the GET request is 200. 通过查看烧瓶日志,我还看到GET请求的状态为200。

Update: I use brave/safari browser. 更新:我使用勇敢/野生动物园浏览器。

I can't figure out where I'm wrong yet. 我不知道我哪里错了。 Any help appreciated! 任何帮助表示赞赏! Thanks 谢谢

The CORS issue can be solved with Flask-CORS extension. 可以通过Flask-CORS扩展程序解决CORS问题。
Install it with pip: 用pip安装它:

$ pip install flask-cors

Then in your API: 然后在您的API中:

from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM