3

I want to do cross domain requests between my backbone app at 127.0.0.1 and and my Flask server at 127.0.0.1:5000. Before I switched to a Backbone I had an Ember.js app and I had the same problem. But I got it working by adding this in my Flask app:

@app.after_request
def after_request(data):
    response = make_response(data)
    response.headers['Content-Type'] = 'application/json'
    response.headers['Access-Control-Allow-Origin'] = 'http://localhost'
    return response

and configure the Ember RESTAdapter this way:

adapter: DS.RESTAdapter.create({ 
    url : 'http://127.0.0.1:5000',
    // In order to allow cross domain requests

    ajax: function(url, type, hash) {
       jQuery.ajax(url)
    }
  })
});

But this does not work with my Backbone application.

XMLHttpRequest cannot load http://127.0.0.1:5000/files. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.

I guess I've to change some settings on the client side. But I don't know what. What do I've to do so that I am able to to do cross domain requests?

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
OrangeTux
  • 11,142
  • 7
  • 48
  • 73

3 Answers3

3

Here is what worked for me.

http://flask-cors.readthedocs.org/en/latest/

$ pip install -U flask-cors

from flask import Flask
from flask.ext.cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route("/")
@cross_origin()
def helloWorld():
  return "Hello, cross-origin-world!"
Dan Rasmuson
  • 5,705
  • 5
  • 29
  • 45
2

I got it working by adding

response.headers['Access-Control-Allow-Headers'] = "Origin, X-Requested-With,Content-Type, Accept" to the after_request() method.

OrangeTux
  • 11,142
  • 7
  • 48
  • 73
0

For development you can:

Or just set Access-Control-Allow-Origin to *

Community
  • 1
  • 1
Vitalii Petrychuk
  • 14,035
  • 8
  • 51
  • 55