I'm trying to send a POST request to localhost and was getting an error about CORS not being allowed. I added an "Access-Control-Allow-Origin: *" header among other things based on what other people have suggested, but am now getting a different error: "ERR_INVALID_HTTP_RESPONSE". I'd really appreciate it if someone could tell me what I'm getting wrong!
server side code
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
client side code
axios.post('http://localhost:9000', {
'cat': 'meow meow meow'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
This is what the console shows:
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)
Am I using do_OPTIONS correctly? What about send_response(200) and end_headers()?