我有许多AJAX请求(使用常规JS制作(,当他们提出我的Python Gae后端的请求时,它们似乎会造成麻烦。这是一个例子:
newGame: function() {
// Calls API to begin a new game, tells view to show placements
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState === XMLHttpRequest.DONE) {
// ... removed unnecessary code for this question
}
};
var requestOjb = {"user_name": battleshipCtrl.user};
xhttp.open('POST', requestPath + 'game', true);
xhttp.send(JSON.stringify(requestOjb));
},
我正在获取带有解析错误的代码400
,但仅在我已部署的服务器上。在开发服务器上一切正常。错误表明问题在于我的后端函数" new_game",但没有指定发生错误的行。当我直接从API Explorer访问它时,端点功能正常工作,因此我认为问题必须是从JS文件发送的数据的结果。无论如何,这就是该功能:
@endpoints.method(request_message=NEW_GAME_REQUEST,
response_message=GameForm,
path='game',
name='new_game',
http_method='POST')
def new_game(self, request):
"""Creates new game"""
user = User.query(User.name == request.user_name).get()
# ... removed unnecessary code for this question
return game.to_form('Good luck playing Battleship!')
请求消息它以{'user_name': 'some_name'}
的形式,并且通过console.log
出现JS以正确的格式发送。
出现解析错误的日志很有趣,因为它显示了200
代码POST
请求,尽管它在我潜入该日志时提到了400
错误。
我已经两次和三重检查了我的代码是否在开发服务器上工作,并且我已经部署了完全相同的代码。我不知道接下来要去哪里去调试此事。任何帮助都将不胜感激。
弄清楚了。我尝试使用jQuery运行AJAX请求,并收到一个略有不同的错误消息,这使我发现我必须设置请求标题,因为它导致服务器以与本应有的方式读取传入数据。现在以下AJAX请求完美地工作。
newGame: function() {
// Calls API to begin a new game, tells view to show placements
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState === XMLHttpRequest.DONE) {
// ... removed unnecessary code for this question
}
};
var requestOjb = {"user_name": battleshipCtrl.user};
xhttp.open('POST', requestPath + 'game', true);
xhttp.setRequestHeader('Content-type', 'application/json');
xhttp.send(JSON.stringify(requestOjb));
},