PS做任务的网站,快速网页开发,西安软件外包公司,凡科网站怎么修改昨天做的网站通过按钮提交get,post请求#xff0c;并且后端响应数据#xff0c;显示到前端 当点击get按钮时
是发起Get请求
后端接收到Get请求后#xff0c;把数据写入到body内 当点击pst按钮时
是发起Post请求
后端接收到Post请求后#xff0c;把数据写入到body内 之后前端就从bod…通过按钮提交get,post请求并且后端响应数据显示到前端 当点击get按钮时
是发起Get请求
后端接收到Get请求后把数据写入到body内 当点击pst按钮时
是发起Post请求
后端接收到Post请求后把数据写入到body内 之后前端就从body内读取数据写入显示到页面上 前端代码
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head!-- 用来接收后端的数据 --div classconnter/div!-- 触发get post请求按钮 --button classGetGET/buttonbutton classPostPOST/buttonbodyscript srchttps://code.jquery.com/jquery-3.7.1.min.js/scriptscriptlet getdocument.querySelector(.Get);get.onclickfunction(){$.ajax({type:get,url:Demo,// 后端传来的数据都在body中success: function(body){//写入数据let connterdocument.querySelector(.connter);connter.innerHTMLbody;}});}let postdocument.querySelector(.Post);post.onclickfunction(){$.ajax({type:post,url:Demo,success:function(body){let connterdocument.querySelector(.connter);connter.innerHTMLbody;}});}/script
/body
/html
后端代码
package Demo;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;WebServlet(/Demo)
public class Demo1 extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType(text/html;charsetutf8);//当接收到get请求时响应数据resp.getWriter().write(Get请求);}Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType(text/html;charsetutf8);//收到post请求响应数据resp.getWriter().write(Post请求);}
}测试1只需要知道客户端是如何发起请求的服务器如何响应数据的即可