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'
});

(摘自博客园

相关推荐
小猪努力学前端6 分钟前
基于PixiJS的小游戏广告开发
前端·webgl·游戏开发
哆啦A梦158812 分钟前
62 对接支付宝沙箱
前端·javascript·vue.js·node.js
用户81686947472524 分钟前
Lane 优先级模型与时间切片调度
前端·react.js
虎头金猫25 分钟前
MateChat赋能电商行业智能导购:基于DevUI的技术实践
前端·前端框架·aigc·ai编程·ai写作·华为snap·devui
LiuMingXin25 分钟前
CESIUM JS 学习笔记 (持续更新)
前端·cesium
豆苗学前端34 分钟前
面试复盘:谈谈你对 原型、原型链、构造函数、实例、继承的理解
前端·javascript·面试
Crystal32844 分钟前
Git 基础:生成版本、撤消操作、版本重置、忽略文件
前端·git·github
lichenyang4531 小时前
React 组件通讯全案例解析:从 Context 到 Ref 的实战应用
前端
姓王者1 小时前
chen-er 专为Chen式ER图打造的npm包
前端·javascript
青莲8431 小时前
Android Jetpack - 2 ViewModel
android·前端