使用Golang服务器是否可以对AngularJS发布请求发出失败?

使用Golang服务器是否可以对AngularJS发布请求发出失败?

问题描述:

I have a HTTP server written in golang and the client is written in AngularJs. The client will send a POST request to the server with username/password to attempt to register a new account. I want the client to do something depending on the answer of the server. If a user with the requested username already exists I want to stay at the registration site, else I want the client to change the view to something else.

I have the follow code to POST to the server, but how do I tell the client that the request failed?

$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')
    });     
};

我有一个用golang编写的HTTP服务器,而客户端是用AngularJs编写的。 客户端将使用用户名/密码向服务器发送POST请求,以尝试注册新帐户。 我希望客户端根据服务器的响应执行某些操作。 如果已经存在具有所请求用户名的用户,我想留在注册站点,否则我希望客户端将视图更改为其他视图。 p>

我将以下代码发布到 服务器,但是如何告诉客户端请求失败? p>

  $ scope.register = function(){
 var postBody = JSON.stringify({username:  $ scope.vm.user.username,密码:$ scope.vm.user.password})
 
 Registration.save(postBody,function(data){
 //成功后\ console.log(data)  
 //$location.path('/bookingsystem')
});  
}; 
  code>  pre> 
  div>

Assuming that you are using $resource to create Registration you can do

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

See the documentation on $resource for more API documentation.