angularjs中的$http.post对我不起作用,$http.get有响应错误

angularjs中的$http.post对我不起作用,$http.get有响应错误,angularjs,spring,rest,Angularjs,Spring,Rest,我是新来的angularjs我正努力学习它,但我面临着一些问题,实际上它们是两个问题: 第一个问题:$http.post永远不会工作,因为没有操作也没有响应。但是,$http.get可以工作 第二个问题:由于第一个问题,我通过$http.get调用restful web服务,但web服务响应状态始终为-1。尽管web服务能够成功地完成其工作,并且总是响应数据为空,但有人能帮我吗 这是我棱角分明的部分: var app = angular.module('myLogin',[]);

我是新来的
angularjs
我正努力学习它,但我面临着一些问题,实际上它们是两个问题:

第一个问题:
$http.post
永远不会工作,因为没有操作也没有响应。但是,
$http.get
可以工作

第二个问题:由于第一个问题,我通过
$http.get
调用restful web服务,但web服务响应状态始终为
-1
。尽管web服务能够成功地完成其工作,并且总是响应数据为空,但有人能帮我吗

这是我棱角分明的部分:

var app = angular.module('myLogin',[]);
                  app.controller('loginController',function($scope,$http){ 
                  $scope.login=function(){ 
                    var username = $scope.username;
                    var password = $scope.pass;

                    $http.get("http://localhost:8080/spring/webservice/login/"+username+"/"+password)

                   .success(function(data,status){
                    alert("data : "+data);
                      alert("Data Inserted Successfully");
                    window.location.href = "chatScreen.html"; 

                  })
                  .error(function(data,status){
                      alert("Status: "+status);
                      window.location.href = "login.html";    

                  });

                      }
                 });
这是我的web服务:

/**
     * web service part
     */
    @RequestMapping(value="webservice/login/{name}/{pass}", method=RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.OK)
    public ResponseEntity<String> weblogin(@PathVariable("name") String name, @PathVariable("pass") String pass)
    {
        System.out.print("username : "+name);
        System.out.print(pass);
        UserService service = new UserService();
        List<UserBean> users = service.getUsers();
        if(users!=null)
        {
            for(UserBean user : users)
                if( ( user.getUsername().equals(name) ) && ( user.getPassword().equals(pass)  ) )
                {
                    System.out.print("success");

                    username = name;
                    //model.addAttribute("result", "Welcome to chat..");                    

                    MessageService messageService = new MessageService();
                    List<MessageBean> messages = messageService.getMessage(username);
                    String userMessages="";
                    if(messages != null)
                    {
                        for(MessageBean msg : messages) 
                            userMessages +="\n"+msg.getSender() + ": " + msg.getMessage()+" \n";
                    }
                    else
                        userMessages +="You have no Messages !";

                    //model.addAttribute("whoSendToMe", userMessages);

                    return new ResponseEntity(HttpStatus.OK);
                }
        }

        return new ResponseEntity<String>("faild", HttpStatus.NOT_FOUND);

    }
/**
*web服务部件
*/
@RequestMapping(value=“webservice/login/{name}/{pass}”,method=RequestMethod.GET)
@ResponseStatus(值=HttpStatus.OK)
公共响应性weblogin(@PathVariable(“name”)字符串名称,@PathVariable(“pass”)字符串pass)
{
系统输出打印(“用户名:”+名称);
系统输出打印(通过);
UserService=newuserservice();
List users=service.getUsers();
如果(用户!=null)
{
for(UserBean用户:用户)
if((user.getUsername().equals(name))&&(user.getPassword().equals(pass)))
{
系统输出打印(“成功”);
用户名=名称;
//model.addAttribute(“结果”,“欢迎聊天…”);
MessageService=newmessageservice();
List messages=messageService.getMessage(用户名);
字符串userMessages=“”;
如果(消息!=null)
{
for(MessageBean消息:消息)
userMessages+=“\n”+msg.getSender()+”:“+msg.getMessage()+”\n”;
}
其他的
userMessages+=“您没有消息!”;
//addAttribute(“whoSendToMe”,userMessages);
返回新的响应状态(HttpStatus.OK);
}
}
返回新的响应状态(“失败”,HttpStatus.NOT_FOUND);
}

参考这可能会让您了解如何解决问题:-

$http({
 method: 'GET',
 url: '/someUrl'
}).then(function successCallback(response) {
   // this is asynchronous call back
  // you will get your data here comming from rest
}, function errorCallback(response) {
  // called asynchronously if an error occurs
});

共享您的代码,以便我们尝试解决它

如果您使用方法GET并收到返回的-1,则通常意味着您给出了错误的URL

对于then POST方法,您应该使用以下语法:

return $http({
            method: 'POST',
            url: 'index.php/email/createDeliverable',
            data: $.param({
                csrfTokenName: --your token--,
                userName: user.name,
                password: password
            }),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        });
记住添加标题部分


您的服务器可能需要CSRF令牌验证,在这种情况下,您需要通过它,请参阅我的示例:csrfTokenName:--您的令牌--,

发布您的代码,请使用其他工具测试您的服务是否可以接收请求?不,我没有,t test it it总是调用errorCallback函数我不知道错误在哪里可能是URL错误,所以它调用错误或者可能是
$http
的方法与REST方法不同。如果你不想要任何棘手的数据,我会建议你使用
.factory
.service
方法。当URL到达web服务并将数据插入数据库时,怎么可能是错误的呢?在我正在处理的项目中,我从一个返回json文件的服务接收到了一个-1,在那里我给出了错误的地址。我想当服务没有真正返回值时,情况可能是一样的。您可能有一个用于在DB中写入的服务,还有一个单独的用于读取的服务?