基于jQuery load()方法的ajax使用

  1. testJQueryLoad.jsp
    <%@ page language=“java” contentType=“text/html; charset=utf-8”
    pageEncoding=“utf-8”%>
    <%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
Insert title here

test jQuery load function.

<button id="button">btnT</button>
<div id="div"></div>
  1. testJQueryLoad.js
    $(document).ready(function(){
    // listener for the button.
    $("#button").click(function(){
    var name=“cyz”;
    $("#div").load(“http://url?name=”+name,
    function(responseText, statusText, xhr){
    processResponse(responseText, statusText, xhr);
    });
    });

});

function processResponse(responseText, statusText, xhr){
if(statusText == “success”) {
alert(“loading success.”);
alert("responseText: " + responseText);
alert("xhr.status: " + xhr.status);
}else if(statusText == “error”) {
alert("error occur: " + statusText);
alert("xhr.status: " + xhr.status);
}
}

  1. handler中的方法
    @RequestMapping("/giveMeSth")
    @ResponseBody
    public String giveMeSth(@RequestParam(value=“name”, required=false)String name){
    if(name == null){
    return “Nothing to give you, cause you do not have a name”;
    }
    System.out.println(“invoke giveMeSth…”);
    return name+": OKOK";
    }

猜你喜欢

转载自blog.csdn.net/weixin_41767051/article/details/86636497
今日推荐