简体   繁体   English

HTTP POST 与 axios: net::ERR_EMPTY_RESPONSE

[英]HTTP POST with axios: net::ERR_EMPTY_RESPONSE

I am trying to make a HTTP Post request through axios:我正在尝试通过 axios 发出 HTTP 发布请求:

    const onSubmit = (values) => {
      console.log("submit", values);
      // ctx.$i18n.locale = "en";
      axios.post("http://192.168.3.2:8081", { // HTTP POST
        name: state.name,
        age: state.age,
        message: state.message,
        canvas: state.canvas,
      });
    };

And my backend server is like (Python):我的后端服务器就像(Python):

class S(BaseHTTPRequestHandler):
    def _set_response(self):
        self.send_response(200)
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Content-type', 'text/html')
        self.end_headers()
    def do_OPTIONS(self):           
        self.send_response(200, "ok")       
        self.send_header('Access-Control-Allow-Origin', '*')                
        self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
        # self.send_header("Access-Control-Allow-Headers", "X-Requested-With")  
    def do_GET(self):
        logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
        self._set_response()
        self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))
    def do_POST(self):
        content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
        post_data = self.rfile.read(content_length) # <--- Gets the data itself      
        logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",str(self.path), str(self.headers), post_data.decode('utf-8'))
        self._set_response()
        self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
        f = open(json.loads(post_data.decode('utf-8'))["name"]+ "-" + date.today().strftime("%m-%d-%Y") + ".txt", "w")
        f.write(post_data.decode('utf-8'))
        f.close()

I tested my backend server with Postman and it works fine, but it keeps returning this error in browser if I tried it with axios我用 Postman 测试了我的后端服务器,它工作正常,但如果我用 axios 尝试它,它会在浏览器中不断返回此错误

POST http://192.168.3.2:8081/ net::ERR_EMPTY_RESPONSE
Uncaught (in promise) Error: Network Error
    at createError (createError.js?2d83:16)
    at XMLHttpRequest.handleError (xhr.js?b50d:84)

BTW: in server loggings, it seems like the server never receives a POST request, but only an OPTIONS request.顺便说一句:在服务器日志记录中,服务器似乎从未收到过 POST 请求,而只是收到了 OPTIONS 请求。

the request created by axios is a promise axios 创建的请求是 promise

you need to chain a .then to it for the request to be actually sent您需要将.then链接到它以实际发送请求

otherwise use the keyword async / await in a newer browser否则在较新的浏览器中使用关键字async / await

there are some examples in the README file of axios: https://github.com/axios/axios axios的README文件中有一些例子: https://github.com/axios/axios

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XHR请求-&gt;获取ERR_EMPTY_RESPONSE - XHR request -> getting ERR_EMPTY_RESPONSE Django添加到购物车按钮返回ERR_EMPTY_RESPONSE错误 - Django Add to Cart Button Returns ERR_EMPTY_RESPONSE error Docker Flask wsl2 ERR_EMPTY_RESPONSE - Docker Flask wsl2 ERR_EMPTY_RESPONSE 我使用cx_freeze和我的烧瓶应用程序,但总是得到ERR_EMPTY_RESPONSE,为什么? - I use cx_freeze with my flask app, but always get ERR_EMPTY_RESPONSE, why? 将自定义中间件添加到settings.py时,Django应用ERR_EMPTY_RESPONSE - Django app ERR_EMPTY_RESPONSE when adding custom middleware to settings.py Web 应用程序不显示任何内容不断返回 ERR_EMPTY_RESPONSE - Web app doens't show anything keeps returning ERR_EMPTY_RESPONSE Python Quart/Hypercorn 流响应导致 net::ERR_HTTP2_PROTOCOL_ERROR 200 - Python Quart/Hypercorn streamed response causing net::ERR_HTTP2_PROTOCOL_ERROR 200 HTTP POST 请求/响应 - HTTP POST request/response 将VueJS与Axios一起使用后响应数据消失 - Using VueJS with Axios post response data disappears 有谁知道为什么来自 IOS 的 HTTP 发布表单上的响应正文为空。 它适用于 Android - Does anyone know why the Response Body would be empty on an HTTP Post form from IOS. It works in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM