我第一次将代码从我的开发机器转移到生产中,而在我的机器上工作的东西™ 正在实时服务器上中断。发生的情况是,当我从试图进入视图中检索Game
实例时,它告诉我是. 不过奇怪的是,如果我将 ajax url 粘贴到浏览器中,视图会正确返回它应该返回的 json。所以调用以下内容:gamedb
game.players[0]
current_player
game
None
http://example.com/current_player/47dd98e9c6aa06ab646d39e516ce54d6db70c2bb/
给出以下正确输出:
["Eve", "e4a70a8e99a4efdfd9331d180cd51798b2291a46"]
我怀疑我设置 ajax 调用的方式有问题,但找不到任何问题。
这是我的整个调用堆栈:
网址.py
url(r'^start/$', 'briscola.views.play')
url(r'^current_player/([a-z0-9]{40})/$', 'briscola.views.current_player')
游戏.js
function currentPlayer(game_uid) {
$.ajax({
type: 'GET',
url: '/current_player/' + game_uid + '/',
success: function(play_data) {
var player_name = play_data[0];
console.log('current player ' + player_name);
}
});
}
视图.py
def start(request):
p1 = Player('Eve')
p2 = Player('Adam')
game = Game([p1, p2])
gamesdb.set(game.uid, game)
players = game.players
output = dict(players=players)
return render_to_response('index.html', output)
@csrf_exempt
def current_player(request, game_uid):
game = gamesdb.get(game_uid)
current_player = game.players[0]
play_data = simplejson.dumps([current_player.name, current_player.uid])
return HttpResponse(play_data, mimetype='application/json')