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

(摘自博客园

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值