2016-01-13 76 views
2

我正在使用vibed.org框架。當我正在處理註銷功能時,出現奇怪的錯誤。從網站註銷錯誤:400 - 錯誤的請求

這裏是我的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: [email protected]:\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");無法訪問。

+0

替換'writeln'與'logInfo',然後再試一次。 – sigod

+0

同樣的問題,爲了避免與'writeln'混淆,我將它改爲'logInfo' –

+0

將'res.writeJsonBody(answerJSON)'放在'else'代碼塊之後。 – sigod

回答

1

Your're發送字符串作爲loginData["username"]代替{username:loginData["username"]}василий