Bottle.py - “请求的资源上不存在 'Access-Control-Allow-Origin' header”

Bottle.py - “No 'Access-Control-Allow-Origin' header is present on the requested resource”

我有以下 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> 连接到 JavaScript 函数,然后调用 API:

<form onSubmit=" return updateMap(event, document.getElementById('weekday').value, document.getElementById('hour').value, document.getElementById('method').value, document.getElementById('normalization').value)">  
...</* Form />...
</form>

而JavaScript函数是:

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...
}

我的问题是,这适用于 mapjson 调用,但不适用于 preddt 调用。每当我 运行 对 mapjson 的任何请求时,它 returns 请求的输出都没有问题,但是,如果我尝试使用参数访问 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.

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

线索是这样的,在您发布的错误消息的末尾:The response had HTTP status code 500

您的 /preddt 路由正在 returning HTTP 500。您对 cp.predicitonCalculator 的调用可能是罪魁祸首。

我很惊讶你没有在你的瓶子控制台中看到错误;也许 Tornado 将它们放在某个日志文件中?我会找一个。

其他简单的尝试:

  • 在没有 Tornado 的情况下尝试 运行,看看服务器错误的原因是否变得更加明显,例如打印在控制台上。

  • 设置瓶子的debug=True。 (您现在将其设置为 False,不知道为什么。)

  • 使用 curl 访问您的服务器,因此您可以检查 HTTP 响应和 headers,比使用浏览器更直接。

  • 为了证明它确实是 cp.predicitonCalculator,将您的 preddt 函数修改为简单的 return 字符串。您将不会再收到 HTTP 500 错误(您的 CORS headers 将正常工作)。

(此外,从长远来看,after_request 挂钩可能不是 set the CORS headers 的最稳健方式。)

祝你好运。