步骤一:把jquery文件放到static目录下
步骤二:resources目录下新建public目录,并在其中新建testAjax.html页面(无法通过浏览器直接访问templates目录下的html页面)
Titlefunction demo() {
$.ajax({
url:"http://localhost:8080/test333",
data:{"id":"1"},
type:"post",
dataType : "json",
/*headers:{
'Content-Type' : 'application/x-www-form-urlencoded;charset=UTF-8'
},*/
success:function(data){
alert("id:"+data.id+" userid:"+data.userid+" pwd:"+data.pwd);
},
error:function () {
alert("失败!");
}
});
}
步骤三:controller层
import com.example.demo.bean.UserBean;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestAjaxController {
@Autowired
private UserService userService;
@RequestMapping(value = "/test333",method = RequestMethod.POST)//produces = "application/json;charset=UTF-8"
public UserBean test333(@RequestParam("id") String id){
UserBean userBean=userService.select(id);
return userBean;
}
}