Ajax傳值的初級學習

Ajax傳值的初級學習

Operation中的創建活動的Ajax傳值爲例進行學習:

1、Operation中的createActivity.html文件中定義了ajax部分:

$.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8', // 很重要
            traditional: true,
            url: "newDEyesPostProxy?appId=00100601_2.1.0&userId=" + vuserId + "&methodName=createActivity",
            data: JSON.stringify(obj),
            async: false,
            success: function (res) {
            },
            error:function(){
            }
})
  • type:’POST’ 或者GET,表示傳值方式
  • traditional true表示阻止深度序列化,後臺可以用getParameterValue()獲取參數
  • url 重要
  • data 將obj對象轉化爲JSON傳遞
  • async 表示是否異步,true表示異步,即同時做好幾件事

2、在newDeyes中的ActivityController.java文件中對參數進行分配

@RequestMapping(value = "/createActivity")
    @ResponseBody
    public void createActivity(HttpServletRequest request, HttpServletResponse response) {
        String appId = request.getParameter("appId");
        Integer type = Integer.parseInt(request.getParameter("type"));
        String name = request.getParameter("name");

        response.setCharacterEncoding("utf-8");

        try {
            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            hashMap.put("appId", appId);
            hashMap.put("type", type);
            hashMap.put("name", name == null ? name : EmojiFilter.filterEmoji(name.replace("\"", "\\\""))); 
            }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章