2017-05-07 3 views
2

Bottle.py - 「いいえ 『のAccess-Controlキーを許可する-起源』ヘッダが要求されたリソース上に存在する」私は、次のボトルルーティングが設定した

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) 

私はを通じてAPI呼び出しを作ってるんですその後、APIを呼び出すJavaScript関数に接続されているHTML <form>

<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のためのすべての要求を実行するたびに、私はパラメータで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

手がかりはあなたが投稿したエラーメッセージの最後に、このです:The response had HTTP status code 500

あなた/preddtルートはcp.predicitonCalculatorへのあなたの呼び出しが犯人である可能性が高いHTTP 500を返しています。

あなたのボトルコンソールにエラーが表示されなかったことに驚いています。たぶん竜巻はいくつかのログファイルに入れているのでしょうか?私は1つを探すだろう。しようとする

その他の簡単なもの:

  • 例えば、サーバエラーの原因がより明らかになったかどうかを確認するために、トルネードせずにそれを実行してみてくださいコンソールに印刷されます。

  • セットボトルdebug=True

  • curlでサーバにヒットすると、HTTPレスポンスとヘッダをブラウザよりも直接調べることができます。

  • 実際にcp.predicitonCalculatorであることを示すには、preddt関数を変更して文字列を返すだけです。あなたはもはやHTTP 500エラーを取得することはありません(CORSヘッダーが機能します)。

(また、長期、after_requestフックがset the CORS headersに最も堅牢な方法ではないかもしれない。)

幸運。

関連する問題