Я пытаюсь отправить POST-запрос на локальный хост, и получаю ошибку о том, что CORS не разрешен.Я добавил заголовок «Access-Control-Allow-Origin: *» среди прочего, основываясь на том, что предложили другие люди, но теперь получаю другую ошибку: «ERR_INVALID_HTTP_RESPONSE».Я был бы очень признателен, если бы кто-то мог сказать мне, что я ошибаюсь!
код на стороне сервера
import BaseHTTPServer
import json
import csv
HOST_NAME = 'localhost'
PORT_NUMBER = 9000
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_OPTIONS(self):
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'POST')
self.send_header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers')
def do_POST(self):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
# send some json as response
код на стороне клиента
axios.post('http://localhost:9000', {
'cat': 'meow meow meow'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Эточто показывает консоль:
spread.js:25 OPTIONS http://localhost:9000/ net::ERR_INVALID_HTTP_RESPONSE
(anonymous) @ spread.js:25
e.exports @ spread.js:25
e.exports @ spread.js:25
Promise.then (async)
r.request @ spread.js:25
r.(anonymous function) @ spread.js:25
(anonymous) @ index.js:20
(anonymous) @ (index):232
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
z @ jquery.min.js:4
(anonymous) @ jquery.min.js:4
load (async)
send @ jquery.min.js:4
ajax @ jquery.min.js:4
n.(anonymous function) @ jquery.min.js:4
onSuccess @ (index):221
connected @ link-initialize.js:1
(anonymous) @ link-initialize.js:1
(index):239 Error: Network Error
at e.exports (spread.js:25)
at XMLHttpRequest.l.onerror (spread.js:25)
Правильно ли я использую do_OPTIONS?Как насчет send_response (200) и end_headers ()?