Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular中的POST请求传递参数,服务端接收不到参数的解决方法 #1

Open
heightzhang opened this issue Jul 6, 2017 · 2 comments
Labels

Comments

@heightzhang
Copy link
Owner

heightzhang commented Jul 6, 2017

原因

image
可以发现传参方式是request payload,参数格式是json,而并非用的是form传参,所以在后台用接收form数据的方式接收参数就接收不到了
  POST表单请求提交时,使用的Content-Type是application/x-www-form-urlencoded,
而此处的Content-Type是:
image

解决

增添两段代码

解决代码如下;

$http({
            method: 'post',
            url: 'http://localhost:8081/search',
            data: {
                "page": page,
                "pageNum": pageNum
            },
           headers: {  //1.设置类型
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
            },
            transformRequest: function(obj) { //2.处理传递参数的格式
                var str = [];
                for (var p in obj) {
                    str.push(encodeURIComponent(p)+"="+encodeURIComponent(obj[p]))
                }
                return str.join('&')
            }
        }).then(function(data) {
   
        }, function(err) {

        });

现在的谷歌监视为:
image

现在传参方式就变成form方式了,然后后端就可以正常接收参数了!

@heightzhang heightzhang changed the title Angular中的POST请求传递参数,后台无法接收的原因 Angular中的POST请求传递参数,服务端接收不到参数的原因 Jul 7, 2017
@heightzhang heightzhang changed the title Angular中的POST请求传递参数,服务端接收不到参数的原因 Angular中的POST请求传递参数,服务端接收不到参数的解决方法 Jul 7, 2017
@Wscats
Copy link

Wscats commented Jul 27, 2017

Angular的post请求后台接受不了数据的解决方法

@heightzhang
Copy link
Owner Author

该方法同样适用于AXIOS的POST请求

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants