javascript - CORS issue with Angular frontend and Python Backend -
javascript - CORS issue with Angular frontend and Python Backend -
i have setup flask based api backend this.
from datetime import timedelta flask import flask, make_response, request, current_app, jsonify functools import update_wrapper app = flask(__name__) def crossdomain(origin=none, methods=none, headers=none, max_age=21600, attach_to_all=true, automatic_options=true): if methods not none: methods = ', '.join(sorted(x.upper() x in methods)) if headers not none , not isinstance(headers, basestring): headers = ', '.join(x.upper() x in headers) if not isinstance(origin, basestring): origin = ', '.join(origin) if isinstance(max_age, timedelta): max_age = max_age.total_seconds() def get_methods(): if methods not none: homecoming methods options_resp = current_app.make_default_options_response() homecoming options_resp.headers['allow'] def decorator(f): def wrapped_function(*args, **kwargs): if automatic_options , request.method == 'options': resp = current_app.make_default_options_response() else: resp = make_response(f(*args, **kwargs)) if not attach_to_all , request.method != 'options': homecoming resp h = resp.headers h['access-control-allow-origin'] = origin h['access-control-allow-methods'] = get_methods() h['access-control-max-age'] = str(max_age) if headers not none: h['access-control-allow-headers'] = headers homecoming resp f.provide_automatic_options = false homecoming update_wrapper(wrapped_function, f) homecoming decorator @app.route('/', methods=['get', 'post']) @crossdomain(origin='*') def landing(): if request.method == 'post': info = request.data homecoming 'god saved post' else: homecoming jsonify(i_am_a='cross domain resource!') if __name__ == '__main__': app.run(host='0.0.0.0', port=8080, debug=true) now making rest calls app backed using angular end in angular container.
$http.get('http://127.0.0.1:8080') .success(function(gdata) { $scope.gdata = gdata; }); var pdata = {'message': 'i post data'}; $http.post('http://127.0.0.1:8080', pdata) .success(function(pdata) { $scope.pdata = pdata; }); my successful, faced cors issue when there post. has got working, need help on this.
try adding ur controller had same problem earlier
$http.defaults.headers.post["content-type"] = "application/x-www-form-urlencoded"; javascript python angularjs flask cors
Comments
Post a Comment