Ajax Fetch get和post请求 一

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>fetch</title>
    <meta name="Keywords" content="关键字">
    <meta name="description" content="简介">
</head>
<body>
<!-- fetch 实现ajax -->
<h3 id="tt"></h3>
<script>
    /*//fetch ajax get 并获取服务器的文本字符串.text()
    fetch("/fetch?id=1&name=徐龙象")
    .then(res=>res.text())
    .then(msg=>{
        document.querySelector("#tt").innerHTML=msg;
    })*/


    //fetch post
    /*转json字符串  JSON.stringify()*/
    fetch("/fetch",{
        method:"post",
        body:"id=1&name=徐凤年",
        headers:new Headers({"Content-Type":"application/x-www-form-urlencoded"})
    })
    .then(res=>res.json())
    .then(msg=>{
        console.log(msg)
    })
</script>
</body>
</html>

Fetch的get和post请求

package ws.wsj.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 卋舉
 * @ProjectName IntelliJ IDEA
 * @ClassName Fetch
 * @description: TODO
 * @date 2021/10/20 9:44
 */
@WebServlet("/fetch")
public class Fetch extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String info = String.format("get-method:%s,hello world",req.getMethod());
        System.out.println(req.getParameter("id"));
        System.out.println(req.getParameter("name"));
        resp.getWriter().println(info);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //服务器输出乱码问题
        //解决post请求,中文乱码
        req.setCharacterEncoding("utf-8");

        String info = String.format("post - method:%s,hello world",req.getMethod());
        System.out.println(req.getParameter("id"));
        System.out.println(req.getParameter("name"));
        resp.getWriter().println(info);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值