2017-03-22 18 views
2

AngularJS with Bottleを使用してクロスドメインJSONPリクエストを実行しようとしていますが、エラーが発生しています。角度Angular JS to BottleへのクロスドメインAngular JSONPリクエスト

// this angular code is on localhost:8888, so it's cross domain 

var URL = "http://localhost:8000/test"; 
    URL = $sce.trustAsResourceUrl(URL); 

$http.jsonp(URL, {jsonpCallbackParam: 'callback'}) 
.then(function successCallback(response) { 
    console.log(response); 
}, function errorCallback(error) { 
    console.log(error); 
}); 

ボトル:

@route('/test') 
def test(): 
    response.headers['Content-Type'] = 'application/json' 
    return json.dumps({"random": "JSON"}) 

エラー:

答えて

1

あなたは(この場合はラッパー関数)JavaScriptアプリケーションを返す必要はありませJSONオブジェクト。このサイトでは、JSONP handlingの基本について説明します。

def jsonp(request, dictionary): 
    if (request.query.callback): 
     # wrap the dictionary in the callback parameter 
     return "%s(%s)" % (request.query.callback, dictionary) 
    return dictionary 

@route('/test') 
    if (request.query.callback): 
     response.content_type = "application/javascript" 
    return jsonp(dict(success="It worked")) 
+0

完璧!ありがとうございました! –

+0

します!何らかの理由でこれまでにそれをすることは決してなかった。 –