带有 axios 的 HTTP POST:net::ERR_EMPTY_RESPONSE
HTTP POST with axios: net::ERR_EMPTY_RESPONSE
我正在尝试通过 axios 发出 HTTP Post 请求:
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,
});
};
我的后端服务器就像 (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()
我用 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)
顺便说一句:在服务器日志记录中,服务器似乎从未收到 POST 请求,而只收到 OPTIONS 请求。
axios 创建的请求是一个 promise
您需要将 .then
链接到它才能真正发送请求
否则在较新的浏览器中使用关键字 async / await
axios的README文件中有一些例子:https://github.com/axios/axios
我正在尝试通过 axios 发出 HTTP Post 请求:
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,
});
};
我的后端服务器就像 (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()
我用 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)
顺便说一句:在服务器日志记录中,服务器似乎从未收到 POST 请求,而只收到 OPTIONS 请求。
axios 创建的请求是一个 promise
您需要将 .then
链接到它才能真正发送请求
否则在较新的浏览器中使用关键字 async / await
axios的README文件中有一些例子:https://github.com/axios/axios