POST w / Params返回400错误

时间:2015-12-22 16:44:27

标签: javascript jquery angularjs spring-mvc

尝试在spring mvc中发送对此方法的回复:

@RestController
public class LoginRController {

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login_Post(@RequestParam(value = "user", required = true) String username,   @RequestParam(value = "pass", required = true) String password) {
        return ("Login: "+username+", "+password);
    }
}

所以我最初尝试过:

控制器

App.controller('LoginController', [
        '$scope',
        'LoginService',
        function($scope, LoginService) {
            var self = this;

            self.authUser = function() {
                LoginService.authUser($('#inputEmail').val(),
                        $('#inputPassword').val()).then(function(d) {
                    console.log("This happened");
                }, function(errResponse) {
                    console.error('Error while fetching Currencies');
                });
            };
        } ]);

服务

App.factory('LoginService', [
        '$http',
        '$q',
        function($http, $q) {

            return {

                authUser : function(username, password) {
                    return $http.post(
                            'login',
                            "user=" + encodeURIComponent(username) + "&pass="
                                    + encodeURIComponent(password)).then(
                            function(response) {
                                return response.data;
                            }, function(errResponse) {
                        console.error('Error @ authUser');
                        return $q.reject(errResponse);
                    })
                }
            }
        } ]);

我发现它总是返回400错误,这意味着所需的字段不匹配...这很奇怪,因为我可以使用Jquery $ post()就好了。

此外,我在此主题AngularJs $http.post() does not send dataHow do I POST urlencoded form data with $http in AngularJS?

上尝试了所有内容

也尝试过:

'use strict';

App.factory('LoginService', [ '$http', '$q', function($http, $q) {

    return {

        authUser : function(username, password) {
            return $http.post('login', {
                params : {
                    user : username,
                    pass : password
                }
            }, {
                headers : {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).then(function(response) {
                return response.data;
            }, function(errResponse) {
                console.error('Error @ authUser');
                return $q.reject(errResponse);
            })
        }
    }
} ]);

1 个答案:

答案 0 :(得分:1)

使用上一个示例作为基线,您需要:1)删除<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="row"> <div class="col-lg-4 col-sm-6 item"> <div class="blogposttwo post-1822 post type-post status-publish format-standard has-post-thumbnail hentry category-music category-48"> <figure> <a href="http://moneyti.co/18-tests2/"> <img src="http://moneyti.co/wp-content/uploads/2015/12/Hydrangeas.jpg" class="img-responsive hovereffect" alt=""> </a> </figure> </div> <!-- Post Title Start --> <h4> <a href="http://moneyti.co/18-tests2/"> ONE Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem IpsuLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,m has been the industry's standard dummy text ever since the 1500s, </a> </h4> <!-- Post Title End --> </div> <div class="col-lg-4 col-sm-6 item"> <div class="blogposttwo post-1816 post type-post status-publish format-standard has-post-thumbnail hentry category-awards category-48"> <figure> <a href="http://moneyti.co/18-tests/"> <img src="http://moneyti.co/wp-content/uploads/2015/12/img16.jpg" class="img-responsive hovereffect" alt=""> </a> </figure> </div> <!-- Post Title Start --> <h4> <a href="http://moneyti.co/18-tests/"> TWO Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, </a> </h4> <!-- Post Title End --> </div> <div class="col-lg-4 col-sm-6 item"> <div class="blogposttwo post-1417 post type-post status-publish format-standard has-post-thumbnail hentry category-news category-48"> <figure> <a href="http://moneyti.co/pardaugavas-ekas-2/"> <img src="http://moneyti.co/wp-content/uploads/2015/11/fb5be-clip-112kb.jpg" class="img-responsive hovereffect" alt=""> </a> </figure> </div> <!-- Post Title Start --> <h4> <a href="http://moneyti.co/pardaugavas-ekas-2/"> THREE Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, </a> </h4> <!-- Post Title End --> </div> <div class="col-lg-4 col-sm-6 item"> <div class="blogposttwo post-1073 post type-post status-publish format-standard has-post-thumbnail hentry category-48 tag-adult tag-fun-video"> <figure> <a href="http://moneyti.co/18-video/"> <img src="http://moneyti.co/wp-content/uploads/2015/12/Jellyfish.jpg" class="img-responsive hovereffect" alt=""> </a> </figure> </div> <!-- Post Title Start --> <h4> <a href="http://moneyti.co/18-video/"> FOUR Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, </a> </h4> <!-- Post Title End --> </div> <div class="col-lg-4 col-sm-6 item"> <div class="blogposttwo post-1073 post type-post status-publish format-standard has-post-thumbnail hentry category-48 tag-adult tag-fun-video"> <figure> <a href="http://moneyti.co/18-video/"> <img src="http://moneyti.co/wp-content/uploads/2015/12/Jellyfish.jpg" class="img-responsive hovereffect" alt=""> </a> </figure> </div> <!-- Post Title Start --> <h4> <a href="http://moneyti.co/18-video/"> FIVE Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, </a> </h4> <!-- Post Title End --> </div> <div class="col-lg-4 col-sm-6 item"> <div class="blogposttwo post-1073 post type-post status-publish format-standard has-post-thumbnail hentry category-48 tag-adult tag-fun-video"> <figure> <a href="http://moneyti.co/18-video/"> <img src="http://moneyti.co/wp-content/uploads/2015/12/Jellyfish.jpg" class="img-responsive hovereffect" alt=""> </a> </figure> </div> <!-- Post Title Start --> <h4> <a href="http://moneyti.co/18-video/"> SIX </a> </h4> <!-- Post Title End --> </div> <div class="col-lg-4 col-sm-6 item"> <div class="blogposttwo post-1073 post type-post status-publish format-standard has-post-thumbnail hentry category-48 tag-adult tag-fun-video"> <figure> <a href="http://moneyti.co/18-video/"> <img src="http://moneyti.co/wp-content/uploads/2015/12/Jellyfish.jpg" class="img-responsive hovereffect" alt=""> </a> </figure> </div> <!-- Post Title Start --> <h4> <a href="http://moneyti.co/18-video/"> SEVEN </a> </h4> <!-- Post Title End --> </div> <div class="col-lg-4 col-sm-6 item"> <div class="blogposttwo post-1073 post type-post status-publish format-standard has-post-thumbnail hentry category-48 tag-adult tag-fun-video"> <figure> <a href="http://moneyti.co/18-video/"> <img src="http://moneyti.co/wp-content/uploads/2015/12/Jellyfish.jpg" class="img-responsive hovereffect" alt=""> </a> </figure> </div> <!-- Post Title Start --> <h4> <a href="http://moneyti.co/18-video/"> EIGHT </a> </h4> <!-- Post Title End --> </div> </div> </div>和2)urlencode数据(此处使用jQuery params):

$.param()

所有解释都是here