如何使用的是post方法该如何传递数据呢?

如何使用的是post方法该如何传递数据呢?

在XMLHttpRequest对象中如何通过POST方法传递数据呢?JSON格式的数据应该怎么生成,formData格式的数据后端是如何进行解析的呢?请老师指点一下

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
提问者 风中随影 2018-11-08 09:52:56
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="resource/js/jquery-3.3.1.js"></script>
<%
    String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
</head>
<body>
    <input type="button" value="查看Java课程" onclick="showJava()">
    <input type="button" value="查看C课程" onclick="showC()">
    <div id="content"></div>
</body>
<script>
    function showJava(){
        var xmlHttp=new XMLHttpRequest();
        xmlHttp.open("POST","<%=basePath %>/ClassesServlet",true);
        xmlHttp.send("flag=1");
        xmlHttp.onreadystatechange=function(){
            if(xmlHttp.readyState=="4"&&xmlHttp.status=="200"){
                var result=xmlHttp.responseText;
                document.getElementById("content").innerHTML=result.html;
            }
        }
    }
     
    function showC(){
        var xmlHttp=new XMLHttpRequest();
        xmlHttp.open("POST","<%=basePath %>/ClassesServlet","true");
        xmlHttp.send("flag=2");
        xmlHttp.onreadystatechange=function(){
            if(xmlHttp.readyState=="4"&&xmlHttp.status=="200"){
                var result=xmlHttp.responseText;
                document.getElementById("content").innerHTML=result.html;
            }
        }
    }
</script>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.imooc.servlet;
 
import java.io.IOException;
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 org.json.JSONObject;
 
@WebServlet("/ClassesServlet")
public class ClassesServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag=request.getParameter("flag");
        JSONObject jsonObject=null;
        if(flag.equals("1")){
            jsonObject=new JSONObject("{html:java基础<br>java Web}");
        else if(flag.equals("2")){
            jsonObject=new JSONObject("{html:C语言基础<br>C语言实战}");
        }
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
 
}


  • 你好同学,我在http://class.imooc.com/course/qadetail/75896这个问答下回复你了,祝学习愉快~
    2018-11-08 10:42:57
慕布斯645313 2018-11-06 10:21:38

一样的,你把GET改为POST及可以了。

http://img1.sycdn.imooc.com//climg/5be0fa980001ee0408820057.jpg

  • 提问者 风中随影 #1
    但是数据如何进行传递呢?可以时候哪些方法进行传递呢?
    2018-11-06 10:52:25
  • 慕布斯645313 回复 提问者 风中随影 #2
    数据传递的方式和之前的有些不一样,下面这个博客我觉的不错,推荐给你看看https://blog.csdn.net/wugewuge/article/details/8048698
    2018-11-06 14:16:00
  • 提问者 风中随影 回复 慕布斯645313 #3
    那我用这个方法写的post方法数据传递为什么会报错呢?一直没有办法吧数据传递过去
    2018-11-08 09:51:55
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码