Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python服务器的javascript客户端:XMLHttpRequest responseText在Get请求后为空_Javascript_Python_Xmlhttprequest_Responsetext_Basehttpserver - Fatal编程技术网

Python服务器的javascript客户端:XMLHttpRequest responseText在Get请求后为空

Python服务器的javascript客户端:XMLHttpRequest responseText在Get请求后为空,javascript,python,xmlhttprequest,responsetext,basehttpserver,Javascript,Python,Xmlhttprequest,Responsetext,Basehttpserver,我正在尝试编写一个能够向python服务器脚本发送/接收数据的chrome扩展。目前,js脚本正在发出GET请求,唯一的问题是.responseText始终为空(即使python脚本使用文本进行响应) popup.js document.addEventListener('DOMContentLoaded', function() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() {

我正在尝试编写一个能够向python服务器脚本发送/接收数据的chrome扩展。目前,js脚本正在发出GET请求,唯一的问题是.responseText始终为空(即使python脚本使用文本进行响应)

popup.js

document.addEventListener('DOMContentLoaded', function() {  
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if (xhr.readyState == xhr.DONE) {           
            var resptxt = xhr.responseText;

            if (resptxt) {
                document.getElementById('texthtm').innerHTML = resptxt;
            } else {
                document.getElementById('texthtm').innerHTML = "no response";
            }
        }
    }

    xhr.open("GET", "http://127.0.0.1:80", true);
    xhr.send();
});
server.py

import time
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

HOST_NAME = "127.0.0.1" 
PORT_NUMBER = 80 

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_HEAD(self):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write("heyyy")


if __name__ == "__main__":

    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
    print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)

    try:
            httpd.serve_forever()
    except KeyboardInterrupt:
            pass

    httpd.server_close()
    print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)

所以每次我按下分机按钮,它只会显示“无响应”,这意味着请求至少已经完成。我做错了什么?

你有没有考虑过这可能是一个CORS的骗局?尝试添加
self.send_头('Access-Control-Allow-Origin','*')
self.send_头('Access-Control-Allow-Headers','Content Type,Authorization')
self.send_头('Access-Control-Allow-Methods','GET,PUT,POST,DELETE')