Question

I am trying to post from a static page that pretends to be a remote web server into my Flask app, running behind uwsgi because of Flask-uWSGI-WebSocket.

Every time I post to my server, the post is blocked because of CORS: XMLHttpRequest cannot load http://127.0.0.1:5000/tnw/post. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I've installed flask-cors and origins is set to * but I still can't get it to go through. What else am I missing here?

My POST: $("#ajaxbutton").click(function() { var json = JSON.stringify(generate_json()); $.post('http://127.0.0.1:5000/tnw/post', json); });

Was it helpful?

Solution

I can see this being one of two problems:

  • You're accessing the page that makes the CORS request over the file:// protocol instead of http://. This causes you to have an origin of 'null'. Try using accessing it via http:// instead (e.g. python -m SimpleHTTPServer).

  • Does your endpoint require cookie-base authentication? jQuery omits cookies on CORS requests by default. If you need to send cookies with your request, add {xhrFields: withCredentials: true} to your ajax requests (docs).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top