I'm kind of new to Python, but I had the same problem as with Node applications. I am making a pretty standard jQuery AJAX request for my local Python:
init: function(callback) { var token = _config.get_token(); $.ajax({ url: 'http://localhost:5000/api/ia/v1/user_likes', type: 'POST', contentType: 'application/json', datatype: 'json', data: token }) .done(function(data) { callback(data); }) .fail(function(err) { callback(err); }); callback(token); }
I can confirm that the variable token is confirmed as follows:
Object {access_token: "791415154.2c0a5f7.4d707361de394512a29682f9cb2d2846", campaign_id: "102"}
But I get this error from my javascript console:
XMLHttpRequest cannot load http://localhost:5000/api/ia/v1/user_likes. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://s3.amazonaws.com' is therefore not allowed access. The response had HTTP status code 500.
I found that when I create a Node application, this is a cors error. The page where I run the JQuery AJAX request is http. Here is the part of my Python code that I believe is incorrectly configured:
from flask import Flask, request, redirect from flask.ext.cors import CORS, cross_origin app = Flask(__name__) cors = CORS(app) app.config['CORS_HEADERS'] = 'application/json'
And the route:
@app.route("/api/ia/v1/user_likes", methods=['POST', 'OPTIONS']) def user_likes(): validate = validate_request(request.data) return 'something'
The My Python error also returns an error, because the request never falls into this line of code:
def validate_request(object_from_user): load_object = json.loads(object_from_user)
I can fix it later. Anyway, does anyone have any suggestions for setting up Cors for Python?