简体   繁体   English

仅在实时服务器上解析来自JS AJAX请求的错误-Python GAE

[英]Parse Error from JS AJAX requests only on live server - Python GAE

I have a number of AJAX requests (made with regular JS) that seem to be causing trouble when they make requests of my Python GAE back end. 我有许多AJAX请求(使用常规JS制作),当它们向我的Python GAE后端发出请求时似乎引起麻烦。 Here's an example: 这是一个例子:

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));
},

I am getting a code 400 with a Parse Error, but only on my deployed server. 我得到的代码为400 ,带有“解析错误”,但仅在部署的服务器上。 Everything works fine on the dev server. 在开发服务器上一切正常。 The error says the problem is with my back-end function "new_game", but does not specify a line where the error occurred. 该错误表明问题出在我的后端函数“ new_game”上,但未指定发生错误的行。 The endpoint function works correctly when I access it directly from the API explorer, so I figure the problem must be a result of the data sent from my JS file. 当我直接从API资源管理器访问终结点函数时,它可以正常工作,因此我认为问题一定是由JS文件发送的数据导致的。 Here's that function anyway: 无论如何,这是该功能:

@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!')

The request message it's expecting takes the form of {'user_name': 'some_name'} and it appears, through console.log , that JS is sending it in the right format. 期望的请求消息采用{'user_name': 'some_name'}的形式,并且通过console.log看起来JS正在以正确的格式发送消息。

The log where the parse error comes up is interesting, because it shows a 200 code POST request, although it mentions the 400 error when I dive into that log. 出现解析错误的日志很有趣,因为它显示了200代码POST请求,尽管当我深入该日志时提到了400错误。

I've double and triple checked that my code works on the dev server, and that I've got the exact same code deployed. 我已经一遍又一遍地检查了我的代码是否可以在开发服务器上运行,并且已经部署了完全相同的代码。 I don't know where to look next to continue debugging this thing. 我不知道接下来要去哪里调试该东西。 Any help is appreciated. 任何帮助表示赞赏。

Figured it out. 弄清楚了。 I tried running the AJAX request with jQuery, and got a slightly different error message, which lead me to find that I had to set the request header, because it was causing the server to read the incoming data differently than it should have. 我尝试使用jQuery运行AJAX请求,并收到一条略有不同的错误消息,这使我发现我必须设置请求标头,因为这导致服务器读取传入数据的方式与应有的方式不同。 The following AJAX request now works perfectly. 现在,以下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));
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM