相关说明
格式说明:
1. 参数名;
2. 参数类型;
3. 参数说明, 默认值, 可选值;
url
String
(默认: 当前页地址) 发送请求的地址.
type
String
(默认: "GET") 请求方式 ("POST" 或 "GET"), 默认为 "GET".
注意: 其它 HTTP 请求方法, 如 PUT 和 DELETE 也可以使用, 但仅部分浏览器支持.
timeout
Number
设置请求超时时间 (毫秒). 此设置将覆盖全局设置.
async
Boolean
(默认: true) 默认设置下, 所有请求均为异步请求. 如果需要发送同步请求, 请将此选项设置为 false. 注意, 同步请求将锁住浏览器, 用户其它操作必须等待请求完成才可以执行.
- beforeSend
- Function
发送请求前可修改 XMLHttpRequest 对象的函数, 如添加自定义 HTTP 头. XMLHttpRequest 对象是唯一的参数.
- function (XMLHttpRequest) {
- this;
- }
- cache
- Boolean
(默认: true) jQuery 1.2 新功能, 设置为 false 将不会从浏览器缓存中加载请求信息.
- complete
- Function
请求完成后回调函数 (请求成功或失败时均调用). 参数: XMLHttpRequest 对象, 成功信息字符串.
- function (XMLHttpRequest, textStatus) {
- }
- contentType
- String
(默认: "application/x-www-form-urlencoded") 发送信息至服务器时内容编码类型. 默认值适合大多数应用场合.
- data
- Object,String
发送到服务器的数据. 将自动转换为请求字符串格式. GET 请求中将附加在 URL 后.
查看 processData 选项说明以禁止此自动转换. 必须为 Key/Value 格式.
如果为数组, jQuery 将自动为不同值对应同一个名称.
如 {foo:["bar1", "bar2"]} 转换为 '&foo=bar1&foo=bar2'.
dataType
String
预期服务器返回的数据类型. 如果不指定, jQuery 将自动根据 HTTP 包 MIME 信息返回 responseXML 或 responseText, 并作为回调函数参数传递, 可用值:
"xml": 返回 xml 文档, 可用 jQuery 处理.
"html": 返回纯文本 HTML 信息; 包含 script 元素.
"script": 返回纯文本 JavaScript 代码. 不会自动缓存结果.
"json": 返回 JSON 数据 .
"jsonp": JSONP 格式. 使用 JSONP 形式调用函数时,
如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名, 以执行回调函数.
error
Function
(默认: 自动判断 (xml 或 HTML)) 请求失败时将调用此方法.
这个方法有三个参数: XMLHttpRequest 对象, 错误信息,(可能) 捕获的错误对象.
- function (XMLHttpRequest, textStatus, errorThrown) {
- // 通常情况下 textStatus 和 errorThown 只有其中一个有值 this;
- }
- global
- Boolean
(默认: true) 是否触发全局 Ajax 事件.
设置为 false 将不会触发全局 Ajax 事件, 如 ajaxStart 或 ajaxStop .
可用于控制不同的 Ajax 事件
- ifModified
- Boolean
(默认: false) 仅在服务器数据改变时获取新数据. 使用 HTTP 包 Last-Modified 头信息判断.
- processData
- Boolean
(默认: true) 默认情况下, 发送的数据将被转换为对象 (技术上讲并非字符串) 以配合默认内容类型 "application/x-www-form-urlencoded".
如果要发送 DOM 树信息或其它不希望转换的信息, 请设置为 false.
success
Function
请求成功后回调函数. 这个方法有两个参数: 服务器返回数据, 返回状态 function (data, textStatus) {
- // data could be xmlDoc, jsonObj, HTML, text, etc...
- }
1. 发送 get 请求将参数通过? 拼接在 url 后面
- $.Ajax({
- url: "/login/authenticate?name="+name+"&user="+user, // 请求的 url 地址
- url: Feng.ctxPath + "/reportDay/checkMerNo?merNo=" + merNo, // 请求的地址
- cache: "false", // 设置为 false 将不会从浏览器中加载请求信息
- async: "true", //true 所有请求均为异步请求
- dataType: "json", // 请求返回数据的格式
- type:"get", // 请求方式
- success: function(data){ // 请求成功后的回调方法
- console.log("data:" + data);
- if(data == true){
- }else{
- Feng.error("不存在该商户号, 请仔细检查后在填写!")
- }
- },
- error:function () { // 请求失败后的回调方法
- Feng.error("请求出错!")
- }
- });
后台接收参数
- /**
- 通过 @RequestParam("***") 接收参数值
- */
- @RequestMapping("/yourUrl")
- @ResponseBody
- public String yourUrl(@RequestParam("merNo") String merNo) {
- System.out.println(yourData);
- // 返回值可以自由定义
- return "SUCCESS";
- }
2. 将参数拼接在 url 中, 后台通过占位符接收参数 /{id}
- $.Ajax({
- url: Feng.ctxPath + "/reportDay/checkMerNo/" + merNo, // 请求的地址
- cache: "false", // 设置为 false 将不会从浏览器中加载请求信息
- async: "true", //true 所有请求均为异步请求
- dataType: "json", // 请求返回数据的格式
- type:"get", // 请求方式
- success: function(data){ // 请求成功后的回调方法
- console.log("data:" + data);
- if(data == true){
- }else{
- Feng.error("不存在该商户号, 请仔细检查后在填写!")
- }
- },
- error:function () { // 请求失败后的回调方法
- Feng.error("请求出错!")
- }
- });
后台接收参数
- /**
- @PathVariable("yourDataName") 接收
- */
- @RequestMapping("/yourUrl/{merNo}")
- @ResponseBody
- public String yourUrl(@PathVariable("merNo") String merNo) {
- System.out.println(yourData);
- // 返回值可以自由定义
- return "SUCCESS";
- }
3. 通过 post 提交方式将 form 表单中的数据序列化后传递到后台.
- $.Ajax({
- url: Feng.ctxPath + "/reportDay/checkMerNo", // 请求的地址
- data: $("#myForm").serialize(), // 对 id 为 myForm 的表单数据进行序列化并传递到后台
- cache: "false", // 设置为 false 将不会从浏览器中加载请求信息
- async: "true", //true 所有请求均为异步请求
- dataType: "json", // 请求返回数据的格式
- type:"post", // 请求方式
- success: function(data){ // 请求成功后的回调方法
- console.log("data:" + data);
- if(data == true){
- }else{
- Feng.error("不存在该商户号, 请仔细检查后在填写!")
- }
- },
- error:function () { // 请求失败后的回调方法
- Feng.error("请求出错!")
- }
- });
后台接收参数
- /**
- 在这里我假设大家表单数据与 User 实体类相对应
- */
- @RequestMapping("/yourUrl")
- @ResponseBody
- public String yourUrl(User user) {
- System.out.println(user.toString());
- return "SUCCESS";
- }
4. 常见的 data 传值
- $.Ajax({
- url: Feng.ctxPath + "/reportDay/checkMerNo", // 请求的地址
- data: { // 提交数据
- "username": "admin", // 前者为字段名, 后者为数据
- "password": "admin"
- },
- cache: "false", // 设置为 false 将不会从浏览器中加载请求信息
- async: "true", //true 所有请求均为异步请求
- dataType: "json", // 请求返回数据的格式
- type:"post", // 请求方式
- success: function(data){ // 请求成功后的回调方法
- console.log("data:" + data);
- if(data == true){
- }else{
- Feng.error("不存在该商户号, 请仔细检查后在填写!")
- }
- },
- error:function () { // 请求失败后的回调方法
- Feng.error("请求出错!")
- }
- });
接收参数
- /**
- 在这里我假设大家表单数据与 User 实体类相对应
- */
- @RequestMapping("/yourUrl")
- @ResponseBody
- public String yourUrl(@RequestParam("username") String username,
- @RequestParam("password") String password) {
- System.out.println("username="+username+";password="+password);
- return "SUCCESS";
- }
5. 拼接 data
- $.Ajax({
- url: Feng.ctxPath + "/reportDay/checkMerNo", // 请求的地址
- data: 'name='+name+'&user='+user, // 参数值
- cache: "false", // 设置为 false 将不会从浏览器中加载请求信息
- async: "true", //true 所有请求均为异步请求
- dataType: "json", // 请求返回数据的格式
- type:"get", // 请求方式
- success: function(data){ // 请求成功后的回调方法
- console.log("data:" + data);
- if(data == true){
- }else{
- Feng.error("不存在该商户号, 请仔细检查后在填写!")
- }
- },
- error:function () { // 请求失败后的回调方法
- Feng.error("请求出错!")
- }
- });
后台接收参数
- /**
- @RequestParam
- */
- @RequestMapping("/yourUrl")
- @ResponseBody
- public String yourUrl(@RequestParam("name") String name, @RequestParam("user") String user) {
- System.out.println("username="+username+";password="+password);
- return "SUCCESS";
- }
6.@ModelAttribute 注解: 使用 @ModelAttribute 这个方法可以直接将参数映射成 pojo 对象, 我不加 @ModelAttribute 注解, 直接接收 pojo 对象, 同样能够接收到参数
- <script type="text/javascript">
- $(function(){
- $.Ajax({
- type:"post",
- url:"http://localhost:8080/test",
- data:{
- "name":"张三",
- "phone":"10086",
- "password":"123456"
- },
- async:true,
- success:function(data){
- console.log(data);
- }
- });
- })
- </script>
后台接收参数
- @RequestMapping("/test")
- @ResponseBody
- public String testUser(@ModelAttribute("TUser") TUser user){
- System.out.println(user.getName());
- System.out.println(user.getPassword());
- System.out.println(user.getPhone());
- return "ok";
- }
- 7.@PathVariabl
- <script type="text/javascript">
- $(function(){
- $.Ajax({
- type:"post",
- url:"http://localhost:8080/test/10086",
- data:{
- "name":"张三",
- "password":"123456"
- },
- async:true,
- success:function(data){
- console.log(data);
- }
- });
- })
- </script>
后台接收参数
- @RequestMapping("/test/{phone}")
- @ResponseBody
- public String testUser(String name,@PathVariable String phone,String password){
- System.out.println(name);
- System.out.println(phone);
- System.out.println(password);
- return "ok";
- }
使用 HttpServletRequest 接收参数
- @RequestMapping("/test")
- @ResponseBody
- public String testUser(HttpServletRequest request, HttpServletResponse response){
- System.out.println(request.getParameter("name"));
- System.out.println(request.getParameter("phone"));
- System.out.println(request.getParameter("password"));
- return "ok";
- }
使用 @RequestParam 接收参数
- @RequestMapping("/test")
- @ResponseBody
- public String testUser(@RequestParam("name") String a,@RequestParam("phone") String b, String password){
- /**
- * @RequestParam() 里边的值必须要和前端传递过来的参数名字相同
- */
- System.out.println(a);
- System.out.println(b);
- System.out.println(password);
- return "ok";
- }
非异步方式传值
非异步方式前台传递参数
1. 与异步方式类似, 使用 form 直接提交或者在链接中拼接参数即可.
- <!-- form 表单提交 -->
- <form id="myForm" class="layui-form" action="/yourUrl" method="post">
- <input type="text" name="username" required placeholder="邮箱"/>
- <input type="password" name="username" required placeholder="密码"/>
- <button type="submit" class="layui-btn"> 登录 </button>
- </form>
- <!-- a 标签拼接参数 -->
- <a href="/yourUrl?youDataName=yourData"> 问号传递参数 </a>
- <a href="/yourUrl/yourData"> 拼接链接传递参数 </a>
2. 后台接受参数方式不变, 与异步方式完全相同.
非异步方式后台向前台传递数据
- // 1. 可以通过 session 进行参数传递
- @RequestMapping("/yourUrl")
- public String yourUrl(HttpServletRequest request) {
- // 通过 request 获取 session, 然后向 session 中放入参数 key-value
- request.getSession().setAttribute("yourDataName", "yourData");
- // 跳转到你的视图
- return "/yourViews";
- }
- // 2. 可以通过 Model 进行参数传递
- @RequestMapping("/yourUrl")
- public String yourUrl(Model model) {
- // 向 model 中加入参数 key-value
- model.addAttribute("yourDataName", "yourData");
- // 跳转到你的视图
- return "/yourViews";
- }
- // 3. 同样可以用 request 进行参数传递
- @RequestMapping("/yourUrl")
- public String yourUrl(HttpServletRequest request) {
- // 通过 request 放入参数 key-value
- request.setAttribute("yourDataName", "yourData");
- // 跳转到你的视图
- return "/yourViews";
- }
来源: https://blog.csdn.net/LuckFairyLuckBaby/article/details/101267520