jquery ajax spring mvc

Ajax 2 ways to submit

   get

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

   post

$.ajax({
            type: "POST",
            url: "Service/SimpleData",
            contentType: "application/json", //must have
            dataType: "json", //represents the return value type, not required
            data: JSON.stringify({ 'foo': 'foovalue', 'bar': 'barvalue' }),  //data: "{'str1':'foovalue', 'str2':'barvalue'}",
            success: function (jsonResult) {
                alert(jsonResult);
          

 

 

Corresponding 2 kinds of spring mvc

   corresponding to the get method

@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;
	}

   Description: @ResponseBody: The json data returned to ajax is annotated with this

              result/ {sn} : {sn} is a path parameter and needs to be used together with @PathVariable("sn")

     method = {RequestMethod.POST, RequestMethod.GET}: Receive get and post methods

     produces = "text/html;charset=UTF-8": matches the request header, Accept: text/html, not often used

             Spring mvc also has a common annotation: @RequestParam( value= "aa" , required= true )String title

 

 Mvc corresponding to post mode

 

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

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326856458&siteId=291194637