简体   繁体   English

如何在Ajax呼叫中发送用户名和密码?

[英]How to send username and password in Ajax call?

Here I want to send username and password to my spring mvc controller. 在这里,我想将用户名和密码发送到我的spring mvc控制器。 I am using ajax to call mvc method.And it is not reaching to controller. 我正在使用ajax来调用mvc方法,并且无法到达控制器。

angular controller:- 角度控制器:-

$scope.formSubmit=function(){
        alert("submitted")

        return $http({
            url :'loginSubmit' ,
            method : "POST",
            data: { username: $scope.username, password: $scope.password }
            //data: data
        }).success(function(data, status, headers){

        }).error(function(data, status, headers) {
            console.log("login error"+status);     
     }); 

    }

mvc controller MVC控制器

@RequestMapping(value="/loginSubmit",method=RequestMethod.POST)
    public  @ResponseBody User loadUserByUsername(@RequestParam("username") String username,@RequestParam("password") String password, HttpSession session){
        System.out.println("controller");
        System.out.println("name is "+username);

        return null;
    }

If you are using request params, you have to append to URL 如果使用请求参数,则必须附加到URL

$scope.formSubmit=function(){
        alert("submitted")

        return $http({
            url :'loginSubmit?username='+$scope.username+'&password='+$scope.password ,
            method : "POST",
        }).success(function(data, status, headers){

        }).error(function(data, status, headers) {
            console.log("login error"+status);     
     }); 

    }

Note: Dont use alert. 注意:请勿使用警报。 Use console.log 使用console.log

The best way to do this would be sending the credentials in body instead of request URL 最好的方法是在正文中发送凭据,而不是请求URL

** Retain your old JS code of sending data as body and Change the JAVA code to** **保留将主体作为数据发送的旧JS代码,并将JAVA代码更改为**

@RequestMapping(value="/loginSubmit",method=RequestMethod.POST)
public  @ResponseBody User loadUserByUsername(@RequestBody Map<String,Object> credentials, HttpSession session){
    String username = credentials.get("username");
    String password = credentials.get("password");
    System.out.println("controller");
    System.out.println("name is "+username);
    return null;
}

Try this 尝试这个

$scope.formSubmit=function(){
        alert("submitted")
        var uname = $scope.username,pswd=$scope.password;
        return $http({
            url :'loginSubmit',
            method : "POST",
            params : {username:uname,password:pswd}
        }).success(function(data, status, headers){

        }).error(function(data, status, headers) {
            console.log("login error"+status);     
     }); 

    }
return $http({
            url :'/loginSubmit',
            method : "POST",
            data: { username: $scope.username, password: $scope.password }
            //data: data
        }).success(function(data, status, headers){

        }).error(function(data, status, headers) {
            console.log("login error"+status);     
     }); 

    }

@RequestMapping(value="/loginSubmit",method=RequestMethod.POST)
    public  @ResponseBody User loadUserByUsername(@RequestParam("username") String username,@RequestParam("password") String password, HttpSession session){
        System.out.println("controller");
        System.out.println("name is "+username);

        return null;
    }

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

相关问题 使用ajax发送用户名和密码是否安全? - Is it safe to send username and password using ajax? 如何发送用户名,密码并单击提交按钮 - How to send username, password and click submit button 如何从客户端发送用于Web服务身份验证的用户名密码 - How to send username password for web service authentication from client side 如何发送 JSP 作为对 AJAX 调用的响应 - How to send JSP as response to AJAX call 如何通过 Ajax 调用向 Servlet 发送文件 - How to send file via Ajax call to Servlet 使用Ajax和Java检索用户名和密码的问题 - Problem of retrieving username and password using Ajax and Java Java-如何验证用户名和密码 - Java - How to Auth Username and Password 如何使用Java在SOAP UI中使用用户名和密码发送SOAP请求 - How to send SOAP request with username and password like in SOAP UI using java 无法弄清楚如何使用用户名/密码从 JSP 发送 HTTP POST 请求 - Can't figure out how to send HTTP POST request from a JSP- with username/password 如何在不提示输入用户名/密码的情况下使用 Windows 身份验证调用 Active Directory? - How can I call Active Directory using Windows Authentication without prompting for username/password?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM