Javascript 仅在实时服务器上解析JS AJAX请求的错误-Python GAE
我有很多AJAX请求(使用常规JS发出),当它们向我的Python GAE后端发出请求时,它们似乎会造成麻烦。下面是一个例子:Javascript 仅在实时服务器上解析JS AJAX请求的错误-Python GAE,javascript,python,ajax,google-app-engine,Javascript,Python,Ajax,Google App Engine,我有很多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 (xh
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
,但仅在部署的服务器上。在dev服务器上一切正常。错误表明问题出在我的后端函数“new_game”上,但没有指定发生错误的行。当我直接从API资源管理器访问endpoint函数时,它可以正常工作,因此我认为问题一定是由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
codePOST
请求,尽管当我深入该日志时它提到了400
错误
我已经反复检查了我的代码是否在dev服务器上工作,并且部署了完全相同的代码。我不知道下一步去哪里继续调试这个东西。感谢您的帮助。解决了这个问题。我尝试用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));
},