HttpServletRequest取值

var params=
{
user_id:'1',
user_token:'',
charset:'',
format:'',
timestamp:'',
sign:'',
request_params:
{
project_id:'',
account_id:'110',
}
}
var obj=JSON.stringify(params);
var project_id=1;
$(function ()
{
$.ajax({
url:'api/account/get_account',
// url:'api/account/test',
type: 'POST',
dataType: 'json',
contentType: "application/x-www-form-urlencoded",
data:params,

@RequestMapping("/get_account")
public String get_account_list(HttpServletRequest request){

    // 公共入参
    int user_id = Integer.parseInt(request.getParameter("user_id"));
    String user_token = request.getParameter("user_token");
    String charset = request.getParameter("charset");
    String format = request.getParameter("format");
    String timestamp = request.getParameter("timestamp");
    String sign = request.getParameter("sign");
    String request_params = request.getParameter("request_params");
    System.out.println("request_params为:"+ request_params);

ajax请求,传递数据,HttpServletRequest如何取request_params的值 , request_params是一个对象

0

5个回答

你应该这么改:
1、将HTTP请求正文格式改成application/json,这样做的目的是将传递数据对象序列化成json字符串写到http请求正文中
2、不要使用getParameter,这个方法你什么也拿不到,使用request读取正文数据转成字符串,这个字符串就是ajax发送的数据了

当然可以使用框架简化代码。

1

request_params不就是个json对象吗,字符串转json对象再取值

2

举个例子:
前端:

$.ajax({
    type : "POST",
    url  : "$baseUrl/assert/uploadAssert",
    data : $("#assertInfo").serialize(),
    async: false,
    success: function(data) {
        if(data.status != 0) {
            window.location.href="$baseUrl/assert";
        }
    }
});

后端:

 @RequestMapping(value = "test", method = RequestMethod.POST)
    @ResponseBody
    public Response<Result> colorTest(@RequestBody RequestModel requestModel) {
        return new Response();
    }
1

@RequestMapping(value = "test", method = RequestMethod.POST)
@ResponseBody
public Response colorTest(@RequestBody RequestModel requestModel) {
return new Response();
}

0

自己的 解决方法 var param=
{
project_id:'1',
account_id:'110'
};
var params=
{
user_id:'1',
user_token:'',
charset:'',
format:'',
timestamp:'',
sign:'',
request_params:JSON.stringify(param) //将内嵌数据转换为JSON格式的字符串
}
var obj=JSON.stringify(params);
var project_id=1;
$(function ()
{
$.ajax({
url:'api/account/get_account',
// url:'api/account/test',
type: 'POST',
dataType: 'json',
// contentType: "application/x-www-form-urlencoded",
data:params,
success:function (data)
{
var html="";
html +="";
html +=" 组合名称";
html +="";

        // data=eval('(' + data + ')');
         data=eval(data);
        for(var i=0;i<data.rows.length;i++)
        {
            var obj=data.rows[i];
            html +="<tr>";
            html +="<td onclick='AccountDetail(\""+obj.account_id+"\",\""+obj.user_id+"\")' style='cursor: pointer'><span >"+obj.account_name+"</span></td>";
            html +="</tr>";
        }
        $("#AccountForProject").html("");
        $("#AccountForProject").html(html);
    }
})

})


后台接受的都是getParameter 都是string格式 所以先将对象转化为string字符串 传递过去,

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!