且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ajax第二例:发送POST|学习笔记

更新时间:2022-01-19 03:38:12

开发者学堂课程【Ajax:ajax第二例:发送POST|学习笔记】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/31

ajax第二例:发送POST请求

目录:

一、 POST注意事项

二、 编写代码​

三、 解决问题

 一、POST注意事项

post请求有一个请求头:Content-Type: application/x-www-form-urlencoded.它有三个注意事项:

1.open: xmlHttp. open "POST"

2.添加一歩:设置content-Typei请求头:

> xmlHttp. setRequestHeader ("ContentType","application/x-www-form-urlencoded");

3.send: xmlHttp. send ("username-zhangSan&password-123");发送请求时指定请求体。

如果发送请求时需要带有参数,一般都用POST请求。

 二、编写代码

1.选中:

public void doGet (HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

system. out.println("Hello AJAX!");

response .getwriter() .print ("Hello AJAX!!!");

加一个方法变为:

public void doPost (HttpServletRequest request,HttpServletResponse response )

throws ServletException, IOException l

System. out.println("(POST: ) Hello AJAX!");

response .getwriter() -print("(POST: ) Hello AJAX!!!");

2.再写ajax2,打开,

修改open方法,指定请求方式为POST。

xmlHttp.open("post", "<c:ur1 value=' /ASezvlet'/>",true;

设置请求头:Content-Type

xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

  send ("uername-zhangSanspassword-123")

ajax第二例:发送POST|学习笔记

3.获取用户名:username

ajax第二例:发送POST|学习笔记

三、解决问题

接下来尝试一下POST

解决想要编码问题:text/html; charset=utf – 8

需要中文:utf – 8,将拼音改成中文。