Java Web

1.GET方式请求

(1).普通URL get请求

|-------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 | http:``//localhost:8080/ajaxGet?id=1&username=用户名&userTrueName=真实姓名 //get也可以传json,通过参数传json字符串,然后后端进行解析(不过一般都不这么做) http:``//localhost:8080/ajaxGet?user={"id":"1","username":"用户名","userTrueName":"真实姓名"} |

(2).表单类GET请求

复制代码
<form id="fromGet" action="fromGet" method="GET">
<input  type="text"name="id"  value="1">
<input  type="text"name="username"  value="用户名">
<input  type="text"  name="userTrueName"  value="真实名字">
</form>

(3).AJAX Get请求

(A)正确示例

|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | $.ajax({ ``type: ``"GET"``, ``url: ``"http://localhost:8080/ajaxGet"``, ``data:{``"id"``:``1``,``"username"``:``"用户名"``,``"userTrueName"``:``"真实名称"``}, ``//contentType:'application/x-www-form-urlencoded' ``contentType:``'application/json' }); |

注意:

1.data必须为json对象

2.实际上无需设置contentType

示例中我故意设置了contentType,但其实不管设置成什么都是无效的,因为传输的数据会在发送请求时,对Json对象进行编码解析

.Form表单提交

ps:针对POST,第一点包含了所有GET请求方式

form表单提交一般说的是content-type为x-www-form-unlencoded或multipart/form-data的请求

(1) 传统form表单提交, 默认content-type为 x-www-form-unlencoded,如下

复制代码
<form id="fromPost" action="fromPost" method="POST">
    <input type="text"name="id" value="1">
    <input type="text"name="username" value="用户名">
    <input type="text" name="userTrueName" value="真实名字">
</form>

(2) 含文件的form表单 ,需要指明enctype为 multipart/form-data(enctype相当于content-type)

复制代码
<form id="fromMutli" action="fromMutli" enctype="multipart/form-data" method="POST">
    <input type="text"name="id" value="1">
    <input type="file" name="file">
</form>

(3) Ajaxform表单提交

复制代码
//data为json对象
$.ajax({
    type: "POST",
    url: "http://localhost:8080/ajaxPost",
    dataType: 'json',
    data:{"id":1,"username":"用户名","userTrueName":"真实名称"},
    contentType:'application/x-www-form-urlencoded'
});

(摘自博客园

相关推荐
excel几秒前
Vue 编译器核心 AST 类型系统与节点工厂函数详解
前端
excel几秒前
Vue 编译器核心:baseCompile 源码深度解析
前端
excel2 分钟前
Vue 编译核心:transformMemo 源码深度解析
前端
excel3 分钟前
Vue 编译核心:transformModel 深度解析
前端
excel3 分钟前
Vue 编译器源码精解:transformOnce 的实现与原理解析
前端
前端架构师-老李3 分钟前
React中useContext的基本使用和原理解析
前端·javascript·react.js
Moonbit5 分钟前
招募进行时 | MoonBit AI : 程序语言 & 大模型
前端·后端·面试
excel7 分钟前
Vue 3 编译器源码深度解析:transformOn —— v-on 指令的编译过程
前端
excel9 分钟前
Vue 编译器核心:transformIf 模块深度解析
前端
CodeToGym14 分钟前
Vue2 和 Vue3 生命周期的理解与对比
前端·javascript·vue.js