如何在Ajax发布请求中成功使用Flask开发服务器?

时间:2019-03-15 16:49:47

标签: python ajax flask

我目前正在使用Flasks的开发服务器,而不是使用单独的开发服务器,因为我不需要额外的体积。我的服务器不会一次处理很多请求,因此我觉得开发服务器已经足够了。

我要做什么

我有一个python脚本,试图通过单击网页上的一个按钮来运行。该网页在Ubuntu上的Apache 2网络服务器上运行。

我做了什么

我已经安装了Flask并尝试发布到我的python脚本中,但是我总是会收到此错误:

function ajaxCall(input) {
    var data = {"param" : input}; 
    $.ajax({
        url: "/cgi-bin/reset.py/request",
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(data),
        dataType: 'json',
        success: function(response){
            console.log("success");
            console.log(response);
        },
        error: function(xhr, status, error) {
            console.log("error");
            var err = xhr.responseText;
            console.log(err);
        }
    });
}

它总是返回到我的Ajax请求中的错误块,而从未返回到成功块。我没有从Python脚本中获得任何返回JSON。我只想知道我的脚本是否运行,我不一定需要脚本本身的任何输出。

P.S。我的Python可能是不正确的,但我什至没有得到它的认可,所以现在一直处于落后状态。我只是想使发布请求成功。

ajax.js

#!/usr/bin/python3
print("Content-type: text/html")
print ("")

from flask import Flask, request, jsonify
import json
app = Flask(__name__)

@app.route('/')
def sendOK(environ, start_response):
  status = '200 OK'
  output = "Hello World!"
  response_headers = [('Content-type', 'text/plain'),('Content-Length', str(len(output)))]
  start_response(status, response_headers)
  return [output]

@app.route('/request', methods=['POST'])
def processRequest():
  data = request.form["param"]
  # other code to do stuff with the data...
  return Response(json.dumps({"status":"success"}), mimetype="application/json")

if __name__=="__main__":
  app.run(debug=True, threaded=True, host="192.168.1.101")

reset.py

{{1}}

它在我的浏览器中返回200(确定),但是我没有任何返回值,我可以知道我的错误块正在执行,而不是成功块。

0 个答案:

没有答案
相关问题