如何使用Ajax交流python-mysql和js?

时间:2018-10-19 15:40:06

标签: javascript python ajax apache

您好,我正在使用xampp在apache服务器中使用python 3.6开发一个简单的应用程序,并且我不使用数据库mysql来简化js代码和python之间的信息。.

这是我的代码:

ajax2.py

$ react-native run-android --help
Scanning folders for symlinks in /Users/Jackson/Sites/fnmultiapptest/node_modules (11ms)

  react-native run-android [options]
  builds your app and starts it on a connected Android emulator or device

  Options:

    --install-debug           
    --root [string]           Override the root directory for the android build (which contains the android directory) (default: "")
    --flavor [string]         --flavor has been deprecated. Use --variant instead
    --variant [string]        
    --appFolder [string]      Specify a different application folder name for the android source. (default: "app")
    --appId [string]          Specify an applicationId to launch after build. (default: "")
    --appIdSuffix [string]    Specify an applicationIdSuffix to launch after build. (default: "")
    --main-activity [string]  Name of the activity to start (default: "MainActivity")
    --deviceId [string]       builds your app and starts it on a specific device/simulator with the given device id (listed by running "adb devices" on the command line).
    --no-packager             Do not launch packager while building
    --port [number]            (default: 8081)
    --config [string]         Path to the CLI configuration file
    -h, --help                output usage information

我的javascrip是:

main.js

#!C:\users\SRT\Local Settings\Application Data\Programs\Python\Python36\python
import sys
import json
import cgi
import mysql.connector
import json

mydb = mysql.connector.connect(
    host = "localhost",
    user = "root",
    passwd = "",
    database = "mydatabase"
) 

fs = cgi.FieldStorage()

sys.stdout.write("Content-Type: application/json")

sys.stdout.write("\n")
sys.stdout.write("\n")
mycursor = mydb.cursor()

sql = "SELECT * from users WHERE name = %s"

result = {}
result['success'] = True
result['message'] = "The command Completed Successfully"
result['keys'] = ",".join(fs.keys())

user = fs.getvalue("name")  #this is correct ???

mycursor.execute(sql,user)
myresult = mycursor.fetchall()

result['data'] = myresult

sys.stdout.write(json.dumps(result,indent=1))
sys.stdout.write("\n")

sys.stdout.close()
HTML中的

仅具有id =“ inputName”的输入,具有id =“ inputPassword”的输入和具有id =“ myButton”的按钮

mysql中的数据库有一个名为varchar,密码varchar和id int的表用户

你能帮我吗?

我想知道如何将数据从js ajax发送到python,使用此信息进行咨询并将结果发送到ajax并在Javascript中使用。

1 个答案:

答案 0 :(得分:0)

解决方案是使用以下表格:

sql = "SELECT * from users WHERE name ='{}' and password = '{}'".format(fs['name'].value, fs['pass'].value)

当我执行mysql查询时,我很好!