首页 > 解决方案 > Javascript调用Python不打印任何东西

问题描述

所以我的目标是将HTML页面接收到的变量传递给python文件,然后分阶段调用python文件,使其将信息插入到SQL数据库中。仅当 javascript 的某个功能最终成功时,才应运行 python。然而,似乎即使知道 python 文件在网络部分运行,尽管在代码的不同点有打印语句,但它什么也不做。Javascript:

if (otp.length == 6 && otp != null) {
    $.ajax({
        url : 'controller.php',
        type : 'POST',
        dataType : "json",
        data : input,
        success : function(response) {
            $("." + response.type).html(response.message)
            $("." + response.type).show();
                console.log(response);
                $.ajax({
                    type: "POST",
                    url: "python.py",
                    data: { response},
                    dataType : "json",
                  }).done(function( response ) {
                      console.log('--response--', response)
                     // do something
                  });
        },
        error : function(data) {
            console.log(data)
            alert("Error encountered. Please try again later.");
        }
    });
}

Python代码

print("Felicitations. La code marche")
import MySQLdb
from flask import Flask,render_template,request
app=Flask(__name__)
@app.route('/')
def index():
   return render_template('add.html')

@app.route('/',methods=['POST'])
def getvalue():
    Gender=request.form['Gender']
    return Gender
    print(getvalue.Gender)

db=MySQLdb.connect("localhost", "name", "password", "fintech")
insertrec = db.cursor()
sqlquery= "INSERT INTO customers(Gender) VALUES('Gender')"
insertrec.execute(sqlquery)
db.commit()
print("Felicitations. La code marche")
db.close()

这是控制台/网络中显示的内容: 此信息不是由 python 打印的,而是由另一个 php 代码打印的,因此无关紧要

网络显示python文件确实在运行,但实际上并没有

标签: javascriptpythonmysqlsql

解决方案


推荐阅读