html 发送delete请求,SpringMVC接收Ajax经过PUT、POST、GET、DELETE传递的参数

1、问题描述

1.今天在进行SSM+RESTful整合的时候,发现经过AJAX发送的POST和GET请求能正常接收,可是没法经过PUT和DELETE传递参数,网上说在web.xml中加入一个监听器,可是这个只能解决PUT传递问题,没有办法解决DELETE问题javascript

2.通过仔细研究以后,找到了一套解决办法,下面把解决办法分享给你们,若是遇到问题请留言~html

2、SpringMVC接收Ajax的四大请求传递参数

2.1 Controller

1.在Controller类的方法中,经过在参数前加上 @RequestBody 注解,将接收的json串放到对象属性中java

a98328b87f4c48d3b44670f231eaa59a.gif

2.2 发送请求

1.GET请求,GET请求一般用来获取数据而不是传递数据,因此数据直接传递便可

a98328b87f4c48d3b44670f231eaa59a.gifjquery

2.在POST、DELETE、PUT中,若是在控制器参数前指定了 @RequestBody 注解,则不能直接经过 data:{"key": value}的形式传递,系统没法解析该类型的参数web

3.对于上述问题咱们须要经过如下步骤传递参数ajax

将须要传递的参数单独写成一个json串存储到一个变量中-用不用引号括起来均可以

var jsonStr = {"id":2, "name": "POST","age": 20};

var jsonStr = '{"id":2, "name": "POST","age": 20}';

须要指定传递的数据类型:contentType: "application/json"

很重要!若是不指定这个控制器的 @RequestBody 注解将没法解析传递的json参数

须要将参数转为JSON数据:data: JSON.stringify(jsonStr)

将咱们提取出来的数据转换为json数据进行传送

2.3 传递PUT参数示例

1.传递put参数,post、delete同样的使用方式spring

a98328b87f4c48d3b44670f231eaa59a.gif

2.完整截图json

Controller

a98328b87f4c48d3b44670f231eaa59a.gif

四大请求

a98328b87f4c48d3b44670f231eaa59a.gif

2.4 源代码

1.Controllerapp

package com.codecoord.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import com.codecoord.dao.PersonMapper;

import com.codecoord.entity.Person;

@RestController

public class PersonController {

// 由于在Spring配置文件中已指定mapper扫描位置,能够自动注入

@Autowired

private PersonMapper personMapper;

/**

* 查询全部数据-返回JSON

*/

@RequestMapping(value="/persons",method=RequestMethod.GET)

public Object persons() {

return personMapper.findAll();

}

/**

* 批量删除数据

*/

@RequestMapping(value="/person", method=RequestMethod.DELETE)

public Object deleteMore(@RequestBody Person person) {

// personMapper.delete(id)

System.out.println("delete: " + person);

return 1;

}

/**

* 添加数据 @RequestBody

*/

@RequestMapping(value="/person", method=RequestMethod.POST)

public Object insert(@RequestBody Person person) {

// personMapper.insert(person)

System.out.println("insert: " + person);

return 3;

}

/**

* 修改数据 @RequestBody

*/

@RequestMapping(value="/person", method=RequestMethod.PUT)

public Object update(@RequestBody Person person) {

System.out.println("update: " + person);

return 4;

}

}

2.四大请求post

pageEncoding="utf-8"%>

index

发起GET请求

发起POST请求

发起PUT请求

发起DELETE请求

/* GET请求 */

$("#sendGet").click(function(){

// 须要传递的参数

$.ajax({

type: "get",// 请求类型

url: "persons",// URL地址

success: function(datas) {// 访问成功

alert(datas);

},

error: function() {// 请求错误

alert("请求失败!");

},

dataType: "json"// 返回值类型

});

});

/* POST请求 */

$("#sendPost").click(function(){

// 须要传递的参数

var jsonStr = {"id":2, "name": "POST","age": 20};

$.ajax({

type: "post",// 请求类型

url: "person",// URL地址

success: function(datas) {// 成功访问

alert(datas);

},

error: function() {// 请求错误

alert("请求失败!");

},

contentType: "application/json",// 传递的数据类型

data: JSON.stringify(jsonStr)// 将参数转为JSON

});

});

/* PUT请求 */

$("#sendPut").click(function(){

// 须要传递的参数

var jsonStr = '{"id":2, "name": "Put","age": 20}';

$.ajax({

type: "put",// 请求类型

url: "person",// URL地址

success: function(datas) {// 成功访问

alert(datas);

},

error: function() {// 请求错误

alert("请求失败!");

},

contentType: "application/json",// 传递的数据类型

data: JSON.stringify(jsonStr)// 将参数转为JSON

});

});

/* DELETE请求 */

$("#sendDelete").click(function(){

// 须要传递的参数

var jsonStr = {"id": 2, "name": "Delete","age": 20};

$.ajax({

type: "delete",// 请求类型

url: "person",// URL地址

success: function(datas) {// 成功访问

alert(datas);

},

error: function() {// 请求错误

alert("请求失败!");

},

contentType: "application/json",// 传递的数据类型

data: JSON.stringify(jsonStr)// 将参数转为JSON

});

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值