My Flask API is supposed to process text query sent in a POST request and respond with a JSON. I send the POST request using the JavaScript fetch API.
function getQueryResults(){
let query = queryInput.value;
statusTextField.innerHTML = "Processing query \"" + query + "\"...";
fetch('http://localhost:5000/', {
method: 'POST',
body: JSON.stringify({"query": query}),
mode: 'no-cors',
}).then((data) => console.log(data))
}
And this is the Flask endpoint (sending a placeholder JSON).
@app.route('/', methods=['POST'])
def get_index():
return Response(json.dumps((1,2,3)), mimetype='application/json')
Strangely, though, the response logged by console.log() contains no body and is of type opaque instead of JSON.
Response {type: "opaque", url: "", redirected: false, status: 0, ok: false, …}
body: null
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 0
statusText: ""
type: "opaque"
url: ""
__proto__: Response
What have I done wrong?