【问题标题】:Parse Error from JS AJAX requests only on live server - Python GAE仅在实时服务器上解析来自 JS AJAX 请求的错误 - Python GAE
【发布时间】:2017-09-25 16:07:18
【问题描述】:

我有许多 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 资源管理器访问端点函数时,它可以正常工作,所以我认为问题一定是从我的 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 错误。

我已经两次和三次检查我的代码在开发服务器上是否可以正常工作,并且我已经部署了完全相同的代码。我不知道下一步该去哪里继续调试这个东西。任何帮助表示赞赏。

【问题讨论】:

    标签: javascript python ajax google-app-engine


    【解决方案1】:

    想通了。我尝试使用 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));
    },
    

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      相关资源
      最近更新 更多