zoukankan      html  css  js  c++  java
  • ajax请求到后台

    方式一:


     1.使用JSON.stringify 将数组对象转化成json字符串;

    var array = ["1", "2"];
    $.ajax({
    type : 'POST',
    url: path + '/check/testPost',
    contentType : "application/json" ,
    data : JSON.stringify(array),
    success : function(data) {} 

    });


    2.后台处理

    @RequestMapping(value = "/testPost", method = {RequestMethod.POST})
    public void testPost(@RequestBody String[] array) throws IOException {
    for (String string : array) {
    System.out.println(string);}
    return ;
    }


    方式二:


    1.前端不做处理:

    var array = ["1", "2"];
    $.ajax({
    type : 'POST',
    url: path + '/check/testPost',
    contentType: "application/x-www-form-urlencoded",
    data: {"array": array},
    success : function(data) {}
    });


    2.后台处理

    @RequestMapping(value = "/testPost", method = {RequestMethod.POST})
    public void testPost(HttpServletRequest req) throws IOException {
    String[] array = req.getParameterValues("array[]");
    for (String string : array) {
    System.out.println(string);
    }
    return ;
    }

    注:两种post请求的content-type不同。

    author:Lik
    Endeavoring to powerless, struggling to move yourself.
  • 相关阅读:
    Web学习之css
    Spring学习之第一个hello world程序
    MySQL基础学习总结
    Jmeter参数化
    mysql慢查询解析-linux命令
    mysql慢查询
    mysql_存储引擎层-innodb buffer pool
    mysql_Qcahce
    memocached基础操作
    Memcached安装配置
  • 原文地址:https://www.cnblogs.com/likwin/p/9242387.html
Copyright © 2011-2022 走看看