zoukankan      html  css  js  c++  java
  • ajax往后台传值的一些方式

    $('#del1').click(function () {
    $.ajax({
    url: 'http://localhost:8089/test1',
    data: {a: 1, b: 2},
    type: 'post',
    success: function (r) {
    console.log(r)
    }
    })
    })
    $('#del2').click(function () {
    $.ajax({
    url: 'http://localhost:8089/test2',
    data: {a: 1, b: 2},
    type: 'post',
    success: function (r) {
    console.log(r)
    }
    })
    })
    //在 jquery 的 ajax 中, contentType都是默认的值:application/x-www-form-urlencoded,这种格式的特点就是,name/value 成为一组,每组之间用 & 联接,而 name与value 则是使用 = 连接。如: wwwh.baidu.com/q?key=fdsa&lang=zh 这是get , 而 post 请求则是使用请求体,参数不在 url 中,在请求体中的参数表现形式也是: key=fdsa&lang=zh的形式。
    $('#del3').click(function () {
    $.ajax({
    url: 'http://localhost:8089/test3',
    data: {id: 1, name: 'zhangsan', sex: '男'},
    type: 'post',
    success: function (r) {
    console.log(r)
    }
    })
    })
    $('#del4').click(function () {
    $.ajax({
    url: 'http://localhost:8089/test4',
    data: JSON.stringify({id: 1, name: 'zhangsan', sex: '男'}),
    contentType: "application/json;charset=UTF-8",
    type: 'post',
    success: function (r) {
    console.log(r)
    }
    })
    })
    $('#del5').click(function () {
    $.ajax({
    url: 'http://localhost:8089/test5?a=1',
    data: JSON.stringify({id: 1, name: 'zhangsan', sex: '男'}),
    contentType: "application/json;charset=UTF-8",
    type: 'post',
    success: function (r) {
    console.log(r)
    }
    })
    })

    $('#del6').click(function () {
    $.ajax({
    url: 'http://localhost:8089/test6',
    data: {arr: [1, 2, 3, 4]},
    type: 'post',
    success: function (r) {
    console.log(r)
    }
    })
    })

    $('#del7').click(function () {
    $.ajax({
    url: 'http://localhost:8089/test7',
    data: JSON.stringify([1, 2, 3, 4]),
    contentType: "application/json;charset=UTF-8",
    type: 'post',
    success: function (r) {
    console.log(r)
    }
    })
    })后台代码:
    @RequestMapping("/test1")
    public String get1(String a,String b){
    return "";
    }
    @RequestMapping("/test2")
    public String get2(@RequestParam("a") String a,@RequestParam("b") String b){
    return "";
    }

    @RequestMapping("/test3")
    public String get3(Person person){
    return "";
    }

    @RequestMapping("/test4")
    public String get4(@RequestBody Person person){
    return "";
    }

    @RequestMapping("/test5")
    public String get5(@RequestBody Person person,@RequestParam("a") String a){
    return "";
    }
    @RequestMapping("/test6")
    public String get6(@RequestParam("arr[]") Integer[] arr){
    return "";
    }

    @RequestMapping("/test7")
    public String get7(@RequestBody Integer[] arr){
    return "";
    }



  • 相关阅读:
    ArrayList源码 (基于1.7)
    java.lang.Class类中的某些方法
    jdk1.8新特性 : 接口中可以有普通方法(非静态方法)和静态方法 , 颠覆了之前我的理解 : 接口中只能有共有常量和抽象方法的概念,后面必须要加一句jdk1.7和1..7之前
    Cesium中级教程10
    Cesium中级教程9
    Cesium中级教程8
    Cesium中级教程7
    Cesium中级教程6
    Cesium中级教程5
    Cesium中级教程4
  • 原文地址:https://www.cnblogs.com/yuanqt/p/11199202.html
Copyright © 2011-2022 走看看