Bottle.py - 请求的资源上显示“No'Access-Control-Allow-Origin'标头”

时间:2017-05-07 12:04:49

标签: javascript python bottle

我有以下瓶子路由设置:

import tornado
from bottle import route, run, hook, response
import cudpred as cp

_allow_origin = '*'
_allow_methods = 'PUT, GET, POST, DELETE, OPTIONS'
_allow_headers = 'Authorization, Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

@hook('after_request') 
def enable_cors():
    '''Add headers to enable CORS'''

    response.headers['Access-Control-Allow-Origin'] = _allow_origin
    response.headers['Access-Control-Allow-Methods'] = _allow_methods
    response.headers['Access-Control-Allow-Headers'] = _allow_headers

@route('/', method = 'OPTIONS')
@route('/<path:path>', method = 'OPTIONS')
def options_handler(path = None):
    return

@route('/mapjson/<weekdaytopredict:int>/<hourtopredict:int>')
def mapjson(weekdaytopredict=0, hourtopredict=0):
    return cp.writeGeoJSON(weekdaytopredict, hourtopredict)


@route('/preddt/<weekdaytopredict:int>/<hourtopredict:int>/<predictionmethod:int>/<normalization:int>')
def preddt(weekdaytopredict=0, hourtopredict=0, predictionmethod =0, normalization=0):
    return cp.predicitonCalculator(weekdaytopredict, hourtopredict, predictionmethod, normalization)

run(server='tornado', host='0.0.0.0', port=2526, debug=False, reloader=True)

我正在通过HTML <form>进行API调用,该HTML <form onSubmit=" return updateMap(event, document.getElementById('weekday').value, document.getElementById('hour').value, document.getElementById('method').value, document.getElementById('normalization').value)"> ...</* Form />... </form> 连接到JavaScript函数,然后调用API:

function updateMap(e, weekday, hour, model, normalization){
    e.preventDefault();
    if (typeof(weekday) == 'undefined' || typeof(hour) == 'undefined' || weekday == '' || hour == '') {
        mapurl = 'mymap.url';
    } else if(model == 0) {
        mapurl = 'http://remote_machine:port/mapjson/' + weekday + '/' + hour
    } else {
        mapurl = 'http://remote_machine:port/preddt/' + weekday + '/' + hour + '/' + model + '/' + normalization
    }
    ...ETC...
}

JavaScript函数是:

mapjson

我的问题是,这适用于preddt电话,但不适用于mapjson电话。每当我运行http://remote_machine:port/preddt的任何请求时,它都会毫无问题地返回请求的输出,但是,如果我尝试使用参数访问XMLHttpRequest cannot load http://remote_machine:port/preddt/4/21/1/0. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://my.hosted.website.com' is therefore not allowed access. The response had HTTP status code 500. ,我会得到:

/

我错过了什么,如何解决这个问题?

0 个答案:

没有答案