【问题标题】:BaseHTTPRequestHandler doPOST method does not update html?BaseHTTPRequestHandler doPOST 方法不更新html?
【发布时间】:2017-01-27 09:20:36
【问题描述】:

我正在尝试使用 python 3.6 获得一个简单的本地服务器。

我启动一个 HTTPServer 并传递一个 BaseHTTPRequestHandler。 do_GET() 方法工作正常。它为执行 POST 请求的 javascript 文件提供服务。

do_POST() 方法在打印“In post”时执行。但是我没有在浏览器中看到写入 self.wfile.write() 的输出。

我错过了什么吗?

from http.server import HTTPServer, BaseHTTPRequestHandler
import json

HOST, PORT = '', 8888
print("Serving HTTP on port %s." % PORT)


class Handler(BaseHTTPRequestHandler):
    def do_POST(self):
        """
        Respond to POST request.
        """

        print("In post")

        self.send_response(200)  # OK
        self.send_header('Content-type', 'text')
        self.end_headers()

        cont = b"post"
        self.wfile.write(cont)

    def do_GET(self):
        """Respond to a GET request."""
        self.send_response(200)

        if self.path == "/":
            self.send_header("Content-type", "text/html")
            self.end_headers()
            path = "index.html"
        else:
            self.send_header("Content-type", "application/javascript")
            self.end_headers()
            path = self.path[1:]
        f = open(path, "rb")
        cont = f.read()
        self.wfile.write(cont)
        f.close()

http = HTTPServer((HOST, PORT), Handler)
http.serve_forever()

【问题讨论】:

    标签: javascript python server


    【解决方案1】:

    如果 POST 请求正在由 Javascript 文件执行,那么您可能正在执行 Ajax 请求。同一个 JS 脚本实际上需要对响应做一些事情; Ajax 的重点在于它不会自动刷新页面。

    【讨论】:

    • 谢谢!你是对的。我通过 $.post("/", data) 调用了帖子,没有回调函数。
    猜你喜欢
    • 2017-01-05
    • 2011-08-18
    • 2018-11-19
    • 2013-02-15
    • 2012-02-27
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    相关资源
    最近更新 更多