简体   繁体   English

Angularjs错误请求(400)

[英]Angularjs Bad Request (400)

I am trying to consume a REST service that I created in a Spring API, when I test it with Swagger it behaves as expected, but when trying to use it from my UI I get a: 我正在尝试使用我在Spring API中创建的REST服务,当我使用Swagger测试它时,它的行为符合预期,但是当尝试从我的UI中使用它时,我得到了:

 rest/login/getCredentials 400 (Bad Request)

I normally use @RequestBody but in this case I used @RequestParam , could that be the source of trouble? 我通常使用@RequestBody但在这种情况下,我使用@RequestParam ,这可能是造成麻烦的原因吗? When I call the service from the UI the thread seems to stop at the controler and the only thing the console shows me is: 当我从UI调用服务时,线程似乎在控制器处停止,控制台向我显示的唯一内容是:

2016-03-10 19:15:02.997  INFO 8852 --- [nio-8090-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-03-10 19:15:03.016  INFO 8852 --- [nio-8090-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms

JS JS

  $scope.recoverPassword = function(){


    $http.post('rest/login/getCredentials',$scope.email).success(function (loginResponse) {

      if(loginResponse.code == 200){
        alert(loginResponse.codeMessage);

      }else{
        alert(loginResponse.codeMessage);
      }

      $scope.showLogin = true;

    });

  };

Controller 控制者

@RequestMapping(value = "/getCredentials", method = RequestMethod.POST)
@Transactional
public LoginResponse getCredentials(@RequestParam("email")String email){

    LoginResponse response = new LoginResponse();
    Boolean state = loginService.getCredentials(email);

    if(state){
        response.setCode(200);
        response.setCodeMessage("Credentials send, please check your email.");
    }else{
        response.setCode(401);
        response.setCodeMessage("Invalid email.");
    }

    return response;

}

The browser's debugger only shows the Request Payload as: 'somemail@mail.com' 浏览器的调试器仅将请求有效负载显示为:“ somemail@mail.com”

I was wondering if someone could point me in the right direction, I'm pretty sure it must be a very dumb mistaken. 我想知道是否有人可以指出我正确的方向,我敢肯定这一定是一个很愚蠢的错误。

EDIT: Changed from @Param to Body for the lols and it worked, still I would like to know why it doesn't work with Param. 编辑:从@Param更改为Body,它起作用了,但我仍然想知道为什么它不适用于Param。

You are using $http.post('rest/login/getCredentials',$scope.email) , as per the api post(url, data, [config]); 您正在根据api post(url, data, [config]);使用$http.post('rest/login/getCredentials',$scope.email) post(url, data, [config]); , you are sending emailId as request content and not as request param. ,您将emailId作为请求内容而不是请求参数发送。 So you controller doesnt get the email address when you use @param. 因此,当您使用@param时,您的控制器不会获得电子邮件地址。

You can also check the network tab in browser, email address is going as post body content not as url request param 您还可以在浏览器中检查“网络”标签,电子邮件地址将作为帖子正文而不是作为url请求参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM