1. 程式人生 > >通過url路徑傳遞引數

通過url路徑傳遞引數

js程式碼


<script>
   var id = 5; 
   boolean flag = false;

   /**重點:ajax只需要type和url屬性*/
   $.ajax({
        async : false,
        cache : false,
        type : 'POST',               
        url : 'area/delete/'+flag+'/'+ id,//(1)請求的action路徑,可以傳遞引數到後臺
        error : function() {
            alert('請求失敗 '
); }, success : function(data) { alert(data); } });
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

java程式碼


import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind
.annotation.ResponseBody; /*接收url中傳來的引數@PathVariable("id") Long id,可以接收基本型別*/ @ResponseBody @RequestMapping(value = "area/delete/{flag}/{id}", method = RequestMethod.POST) public String deleteArea(@PathVariable("id") Long id,@PathVariable("flag") boolean flag) { //處理引數 return "成功"; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

通過ajax的data屬性

js程式碼

ajax前臺往後臺傳引數,前臺是字串,後臺是陣列接收,那麼框架會根據逗號來字串分割成陣列 看引數ids

<script>
   var postData= { //(2)傳遞引數到後臺,注意後臺接收方式 
            "param1": "param1",
            "areaId":2,
            "deleteId":3,
            "ids" : "254,249,248"
   /**重點:ajax的type,url,dataType,data屬性*/
    $.ajax({
            async : false,
            cache : false,
            type : 'POST',
            url : 'area/delete',
            dataType : "json",
            data : postData,            
            error : function() {
                alert('請求失敗 ');
            },
            success : function(data) {
                alert(data);
            }

        });
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

java程式碼

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

/*接收data屬性中傳來的引數:2種方法 引數名和前臺的鍵名保持一致,@RequestParam*/ 
@ResponseBody
@RequestMapping(value = "area/delete", method = RequestMethod.POST)
public String editAreaWithFile(String param1,Long areaId,@RequestParam("deleteId") Long deleteId,Long[] ids) {
        //處理引數
         //這時ids陣列為[254,249,248]
        return "成功";
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

或者

import javax.servlet.ServletRequest;

@ResponseBody
@RequestMapping(value = "area/delete", method = RequestMethod.POST)
public String getSections(ServletRequest request) {

    //用ServletRequest接收引數
    String strRailwayId = request.getParameter("param1");
    String strAreaId = request.getParameter("areaId");
    Long areaId= new Long(strAreaId );
    String strDeleteId = request.getParameter("deleteId");
    Long deleteId= new Long(strDeleteId );
     //處理引數 
    return "成功";
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

通過ajax.data屬性傳遞多個引數

js程式碼

//第一步:定義json格式資料
var postData = {
        "param1" : "param1",
        "areaId" : 2,
        "deleteId" : 3
  };
/**ajax的type,url,dataType,contentType,data屬性*/
$.ajax({
        async : false,
        cache : false,
        type : 'POST',
        url : 'area/delete',
        dataType : "json",
        contentType : 'application/json', //第二步:定義格式
        data : JSON.stringify(postData), //第二步;把json轉為String傳遞給後臺
        error : function() {
                alert('請求失敗 ');
        },
        success : function(data) {
            alert(data);
        }
 });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

java程式碼

/**通過@RequestBody接收引數*/
@ResponseBody
@RequestMapping(value = "area/delete", method = RequestMethod.POST)
public String editAreaWithFile(@RequestBody Map<String, Object> map) {
    String param1 = (String) map.get("param1");
    Integer strAreaId = (Integer) map.get("areaId");
    Integer strDeleteId = (Integer) map.get("deleteId");
    System.out.println("收到的引數==" + param1 + strAreaId + strDeleteId);
    // 處理引數
    return "成功";
}