jquery遇上Ajax

        .当jquery遇上ajax,一切都变了.....

  1. json格式:(k:v)

    {"keyHello","valueHeloo"}


  2. jquery.ajax函数

       前端页面书写:

  3. <title>学习Ajax</title>
    <body>
    <input type="button" id="100" value="ajax学习" onclick="ajaxdemo(this)" />
    <script type="text/javascript">
    	function ajaxdemo(obj){
    		var dataid=obj.id;
    		jQuery.ajax({
    			url:"/sample/coursetudent2/ajaxdemo",
    			type:"post",
    			dataType:'json', 
    			async:true,
    			 data:{"studntid":"100",
    				 "corseid":"100"
    				 },
    			success:function(d){
    				alert("this is :"+d);
    			},
    			error:function(){
    				alert("传输错误!");
    			}
    			});
    	}
    </script>
    </body>
  4. SpringMVC的Controller书写:

@RequestMapping(value="ajaxdemo",method={RequestMethod.POST})
	public void ajaxdemo(HttpServletRequest req,HttpServletResponse resp)
			throws Exception {
		resp.getWriter().write(req.getParameter("studntid")+req.getParameter("corseid"));
	}

总结一下:

        1.使用异步提交数据,后端通过流的形式,向外输出字符串;

        2.显然,浏览器会将全部的流数据并封装到success:function(data){}的data中;



你可能感兴趣的:(jquery遇上Ajax)