2016-05-09 43 views
0

我有一個用golang編寫的HTTP服務器,客戶端是用AngularJs編寫的。客戶端將使用用戶名/密碼向服務器發送POST請求,以嘗試註冊新帳戶。我希望客戶根據服務器的回答做些事情。如果具有請求的用戶名的用戶已經存在,我想留在註冊站點,否則我希望客戶端將視圖更改爲其他內容。是否有可能通過Golang服務器向AngularJS發佈請求失敗?

我有以下代碼發佈到服務器,但如何告訴客戶端請求失敗?

$scope.register = function(){ 
    var postBody = JSON.stringify({username:$scope.vm.user.username, password:$scope.vm.user.password}) 

    Registration.save(postBody, function(data){ 
     //on success 
     console.log(data) 
     //$location.path('/bookingsystem') 
    });  
}; 
+0

你可以考慮使用http狀態碼(https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html),例如'302'用於重定向,'201'用於創建 – ch33hau

回答

0

假設你正在使用$資源創建Registration你可以做

Registration.save(postBody, function(data, responseHeaders){ 
    //on success 
    console.log(data) 
    console.log(responseHeaders) 
    //$location.path('/bookingsystem') 
}, function(httpResponse){ 
    // Handle the error 
    console.log(httpResponse) 
}); 

參見$資源documentation更多的API文檔。

+0

我用$資源。我在服務器端添加了ch33hau提到的http狀態碼,並通過檢查狀態碼在客戶端處理它。 –

相關問題