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

(摘自博客园

相关推荐
万少9 小时前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
橙序员小站11 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
炫饭第一名13 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫14 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊14 小时前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter14 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折14 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_14 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
Angelial14 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js
jiayu15 小时前
Angular学习笔记24:Angular 响应式表单 FormArray 与 FormGroup 相互嵌套
前端