Flask中的错误请求

时间:2017-08-21 21:57:51

标签: python ajax flask

当我通过AJAX向python请求某些内容时,一切都在python中运行正常,但是,当路由返回我的AJAX信息时,只需创建另一个空白页面,其中包含我返回的文本,而不是我想要的内容。需要的页面。 这里是响应图片 image2 这是代码:

@app.route('/', methods=['POST', 'GET'])
def bot():
    texto = request.form['text']
    print(texto)
    conversation = ConversationV1(
      username='a33eb2c9-d218-4e05-a8ff-a46b59c5c3b1',
      password='VATP3XEHsrPL',
      version='2017-05-26'
    )

    context = {}

    workspace_id = '96cbce3b-2fd3-49b0-ad57-da62c33547ee'   

    user = texto
    response = conversation.message(
      workspace_id=workspace_id,
      message_input={'text': user},
      context=context
    )
    context = response['context']

    #se há intenções e dialogo
    if response['intents'] or response['entities']:

        if response['intents']:
            intent = (json.dumps(response['intents'][0]['intent'],indent = 2))
            intent = intent[1:-1]

        dialog = (json.dumps(response['output']['nodes_visited'][0],indent = 2))
        dialog = dialog[1:-1]

        #se há resesposta à intenção
        if response['output']['text']:
            resposta = json.dumps(response['output']['text'][0],sort_keys=True, indent=4)
            resposta = resposta[1:-1]
            resposta = resposta.encode('utf-8')
            resp = (resposta.decode('unicode-escape'))

        #se entrou no nó x,y,z
        if dialog == "perfil_e_id":
            ID = (json.dumps(response['entities'][0]['value'],indent = 2))
            ID = ID[1:-1]
            print (ID)
            resp = perfill(ID)

        elif dialog == "perfil":
            return resp
            ID = texto
            print ("Ikrl : " + ID)
            resp = perfil()

        elif dialog == "heroi":
            ID = (json.dumps(response['entities'][0]['value']))
            ID = ID[1:-1]
            resp = herostats(int(ID))
            return resp

        elif dialog == "counterheroi":
            ID = (json.dumps(response['entities'][0]['value']))
            ID = ID[1:-1]
            resp = herostats(int(ID))
            return resp

    else:
        resposta = json.dumps(response['output']['text'][0],sort_keys=True, indent=4)
        resposta = resposta[1:-1]
        resposta = resposta.encode('utf-8')
        resp = (resposta.decode('unicode-escape'))

    print (resp)
    return resp

我的ajax代码:

$(function(){
    $(".mytext").on("keyup", function(e){
        if (e.which == 13){
            var text = $(this).val();
            if (text !== ""){
                insertChat("me", text);
                document.getElementById("nome").innerHTML = text;              
                $(this).val('');
            }
            $.ajax({
              url: '/',
              data: $(text),
              type: 'POST',

              success: function(response){
                console.log(response);
                insertChat("you", response)

              },
              error: function(error){
                console.log(error)
              }
            })
        }
    });
  });

如果您需要在电脑上进行测试,我会上传.rar文件 https://drive.google.com/open?id=0B9TVHd_5neJ1amdqZWVrbm9mX2c

1 个答案:

答案 0 :(得分:0)

您尚未取消默认提交操作。

if (e.which == 13){
    e.preventDefault();
    ...