【问题标题】:Logout from site cause error: 400 - Bad Request从站点注销导致错误:400 - 错误请求
【发布时间】:2016-01-13 17:01:29
【问题描述】:

我正在使用 vibed.org 框架。当我处理注销功能时,出现奇怪的错误。

这是我的 App.d 代码:

void main()
{
    auto router = new URLRouter;
    router.any("/checkAuthorization", &checkAuthorization);
    router.any("/login", &login);
    router.any("/logout", &logout);
    // ..
}

...

    void logout(HTTPServerRequest req, HTTPServerResponse res)
    {
        logInfo("Logout section");
        Json request = req.json;
        Json answerJSON = Json.emptyObject;         
        if (req.session) // if user have active session
        {
            res.terminateSession();
            answerJSON["status"] = "success";
            answerJSON["isAuthorized"] = false;
            res.writeJsonBody(answerJSON);

            logInfo(answerJSON.toString);

            logInfo("User %s logout", request["username"]); //
        }
    else
    {
        answerJSON["status"] = "fail"; // user do not have active session?
        logInfo("User do not have active session"); 
    }
}

以及 Vue.JS 代码:

  function logout()
  {
      var loginData = new Object();
      //data that we take from user input
      loginData["username"] = this.username; // username more then enough
      console.log("Logout username -> " + loginData["username"]);

    this.$http.post('http://127.0.0.1:8080/logout', loginData["username"]).then(function (response) {
        console.log("server response: ", response.data)
        if(response.data["isAuthorized"] == false)
        {
          console.log("Logout from site success");
          App.topMenuView = 'guestmenu' //Change current view!
          userLoginNotification("Goodbye, " + loginData["username"], "User Logout"); // notificate user
        }
    });

  }

但我遇到 Chrome 控制台错误:

Uncaught (in promise) Object {request: Object, data: "400 - Bad Request Bad Request Internal error information: std.json.JSONException@C:\vibe-d-0.7.27-alpha.1\source\vibe\data\json.d(1100): (0): Error: Expected 'true', got 'test'. ---------------- 0x0044ABF0 in pure @safe bool std.exception.enforceEx!(std.json.JSONException).enforceEx!(bool).enforceEx(bool, lazy immutable(char)[], immutable(char)[], uint) core.thread.Fiber.run()", status: 400, statusText: "Bad Request", ok: false}

而且我不明白出了什么问题。看起来这是服务器端的问题,因为logInfo("Logout section"); 无法访问。

【问题讨论】:

  • writeln 替换为logInfo 并重试。
  • 同样的问题,为了防止与writeln混淆,我将主题更改为logInfo
  • res.writeJsonBody(answerJSON); 放在else 代码块之后。
  • 尝试使用post 而不是any 代替/logout
  • 我尝试将 res.writeJsonBody(answerJSON); 放入 else 和之后 - 结果相同。将any 更改为post 的变体也不起作用。

标签: javascript http d vue.js vibed


【解决方案1】:

您将字符串发送为loginData["username"] 而不是{username:loginData["username"]} василий

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2020-02-03
    • 2016-10-13
    • 2021-03-31
    • 1970-01-01
    • 2018-11-06
    • 2019-06-15
    相关资源
    最近更新 更多