jquery ajax spring mvc

本文详细介绍了两种常见的AJAX提交方式:GET和POST,并展示了如何通过jQuery实现这两种请求。同时,针对每种请求方式,给出了相应的SpringMVC控制器示例,包括路径变量的使用、请求体的数据解析等关键内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ajax 2种提交方式

   get

$.ajax({     
	        type: "Get",     
	        url: "/xiangmu/user/preparPayWeixin/result/"+out_trade_no+".action",      
	        success: function(data) {
	        	if(data.xxx== 'success'){
		            //具体业务
	        	}
	        },     
	        error: function(err) {     
	        	
	        }
	    });

   post

$.ajax({
            type: "POST",
            url: "Service/SimpleData",
            contentType: "application/json", //必须有
            dataType: "json", //表示返回值类型,不必须
            data: JSON.stringify({ 'foo': 'foovalue', 'bar': 'barvalue' }),  //data: "{'str1':'foovalue', 'str2':'barvalue'}",
            success: function (jsonResult) {
                alert(jsonResult);
          

 

 

对应的2种spring mvc

   对应get方式的

@Controller
@RequestMapping("/user/preparPayWeixin")
public class PreparPayNotifyWeixinController extends BaseController{
		
	@ResponseBody
	@RequestMapping(value="result/{sn}",produces = "text/html;charset=UTF-8",method = {RequestMethod.POST, RequestMethod.GET})
	public String getResult(@PathVariable("sn") String sn){
		String re = "";
		if(payStatus.get(sn) != null && "success".endsWith(payStatus.get(sn))){
			re = "{\"data\":\"success\"}";
		}else{
			re = "{\"data\":\"fail\"}";
		}

		return re;
	}

   说明:@ResponseBody:返回给ajax的json数据用此注解

              result/{sn}:{sn}为路径参数,需要和@PathVariable("sn")一起使用

     method = {RequestMethod.POST, RequestMethod.GET}:接收get和post方式

     produces = "text/html;charset=UTF-8":匹配请求头,Accept:text/html,不常使用

             spring mvc还有一个常用注解:@RequestParam(value="aa", required=true)String title

 

 对应post方式的mvc

 

@RequestMapping(value = "/SimpleData",method=RequestMethod.POST)
@ResponseBody
public ActionResult SimpleData(string foo, string bar){            
    return Json("SimpleData", JsonRequestBehavior.AllowGet);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值