1.前台的form表单建立,注意action、enctype的内容,

2.通过添加一个隐藏的iframe标签使form的target指向iframe来达到不跳转页面的效果,同时需要在js里获取iframe里的内容(即后台利用GSON传回来的返回值)。

代码部分:

<form id="form1"  action="../PublishPostingsServlet"  enctype="multipart/form-data"  method="POST"  target="iframe_userInterface">

<!-- 正文区域--多行文本框 -->

<textarea name="ptext" id="ptext" cols="30" rows="10"></textarea>

<!-- 图片和标签选择区域 -->

<ul id="ptext_ul">

<li>

<a id="photo" href="javaScript:;" onclick="showPic();">

<em class="iconfont"></em>图片

</a>

</li>

<li id="lable">

<a href="javaScript:;" onclick="showLable();">

<em class="iconfont"></em>标签

</a>

</li>

</ul>

<div id="tupianqu">

<span class="ziti">本地上传</span>

<input id="tupian_btn" name="tupian_btn" type="file" accept="image/gif,image/jpeg,image/jpg,image/png" onchange="selectFile();" />

</div>

<button id="ptext_btn" type="submit">发布</button>

</form>

<iframe  id="iframe_userInterface" name="iframe_userInterface" style="display: none;"></iframe>

3.js里获取文本代码如下:

$("#iframe_userInterface").load(function(){

var text = $(this).contents().find("body").text();//获取iframe里的内容

console.log(text);//打印iframe页面的内容

}

})

可以利用text来进行验证

后台要接收form表单传过去的数据,并且利用GSON将返回值传回到iframe里,

代码:

@WebServlet("/PublishPostingsServlet")

@MultipartConfig // 标识Servlet支持文件上传

public class PublishPostingsServlet extends HttpServlet {

private static final long serialVersionUID = 1L;// 主要用于版本控制

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// 设置数据的编码方式为utf-8

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

// 返回的是项目在tomcat中的实际发布运行的根路径

String savePath = request.getServletContext().getRealPath("/picture");

Part part = request.getPart("tupian_btn");

String header = part.getHeader("content-disposition");// 获取请求头--form-data; name="tupian_btn"; filename=""

String fileName = getFileName(header);// 获取文件名

System.out.println("文件名:" + fileName);

part.write(savePath + File.separator + fileName);// 获取文件类型

/*判断登录状态*/

String id = null;

int result = 0;// 返回给前端的结果

HttpSession session = request.getSession();

id = (String) session.getAttribute("Account");

System.out.println("session里的id:" + id);

if (id == null) {

result = 4;// 当id为空的时候,登录失效,返回4

}

String ptext = request.getParameter("ptext");// 获取前台页面传递的参数

String label = Tools.getTable(ptext);

String ptime = Tools.getTime();

while (result == 0) {

result = PostingsService.publishPostings(id, ptext, ptime, label, fileName);// result接收数据在处理的结果1或2或3

}

Gson gson = new Gson();

String postinfsInfo = gson.toJson(result);// 定义postingsInfo存放GSON要传回的数据

response.getWriter().write(postinfsInfo);// 返回数据

}

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);// 调用doGet

}

}

关于form表单提交数据后不跳转页面+ajax接收返回值的处理的更多相关文章

  1. 向后台提交数据:通过form表单提交数据需刷新网页 但通过Ajax提交数据不用刷新网页可通过原生态Ajax或jqueryAjax。Ajax代码部分

    原生态Ajax提交表单:需要借助XMLHttpRequest对象的open,要收通过post发送请求还要setRequsetHeader,然后把数据发送给后端,代码如下 目录结构 index.py代码 ...

  2. thinkPHP5.0使用form表单提交数据和删除文章,不用TP的提示页面,使用弹出提示信息

    form表单提交数据和删除文章时,TP的默认信息提示页面的看起来不是很好看,想要实现弹窗提示怎么做呢? 前端:可以使用前端的一个知识--iframe,iframe元素会创建包含另外一个文档的内联框架: ...

  3. js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题

    js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题 js模拟form表单提交数据源码: /** * js模拟form表单提交 * @param ...

  4. springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据

    springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...

  5. Form表单提交数据的几种方式

    一.submit提交 在form标签中添加Action(提交的地址)和method(post),且有一个submit按钮(<input type='submit'>)就可以进行数据的提交, ...

  6. form表单提交数据的数据格式

    form表单提交的数据格式默认是 enctype="application/x-www-form-urlencoded"这样将input框的数据与input框的name属性以键值对 ...

  7. form表单提交没有跨域问题,但ajax提交存在跨域问题

    浏览器的策略本质是:一个域名下面的JS,没有经过允许是不能读取另外一个域名的内容,但是浏览器不阻止你向另外一个域名发送请求. 所以form表单提交没有跨域问题,提交form表单到另外一个域名,原来页面 ...

  8. 关于AJAX与form表单提交数据的格式

    一 form表单传输文件的格式: 只有三种: multipart/form-data 一般用于传输文件,图片文件或者其他的. 那么其中我们默认的是application/x-www-form-urle ...

  9. asp.net.mvc 中form表单提交控制器的2种方法和控制器接收页面提交数据的4种方法

    MVC中表单form是怎样提交? 控制器Controller是怎样接收的? 1..cshtml 页面form提交 (1)普通方式的的提交

随机推荐

  1. TensorFlow 中的张量,图,会话

    tensor的含义是张量,张量是什么,听起来很高深的样子,其实我们对于张量一点都不陌生,因为像标量,向量,矩阵这些都可以被认为是特殊的张量.如下图所示: 在TensorFlow中,tensor实际上就 ...

  2. finished with exit code -1073740791 (0xC0000409)解决方案

    1.在用keras框架跑NER的train时,而且只是在用了keras_contrib.layers的CRF时出现问题: 遇到无错跳出finished with exit code -10737407 ...

  3. Flutter 基础布局Widgets之Align

    Align的作用是为了设置子child的对齐方式,一般作为其他控件的一个参数. 构造函数 const Align({ Key key, this.alignment = Alignment.cente ...

  4. IP unnumbered interface,某个接口不编号,某个接口不分配IP地址

    OSPFv2中,提到点到点链路可以是unnumbered,不编号,不分配IP地址 12.4.1.1.  Describing point-to-point interfaces             ...

  5. 开源堡垒机jumpserver的配置和使用

    开源跳板机jumpserver配置和使用 http://docs.jumpserver.org/zh/docs/quick_start.html#id9 系统设置 基本设置 # 修改url 的&quo ...

  6. Centos7 LVM扩容实例

    Centos7 lvm 扩容与以往版本有所不同   1.插入硬盘,我是在虚拟机上做的测试  直接添加一块5G的硬盘   2.系统读取硬盘信息     # echo "- - -" ...

  7. python3安装pycrypto

    这几天想用py3弄一个系统,需要用到WeChat-sdk这个包,在pip install wechat-sdk的时候报了一系列的错误,最后定位是安装pycrypto出错,各种度娘之后说要安装vs201 ...

  8. c++ bool

    bool 就两个值,真或者假,通常用来存储关系表达式或者逻辑表达式的结果. 以前是用 int 来表示真假,大 int 有多个值,所以才规定 0 为假,非零为真,导致对应关系比较麻烦,有了 bool 就 ...

  9. Asp.net Core MVC(三)UseMvc设置路由

    在家办公,下班继续看点东西,不废话,继续看MVC的路由. asp.net核心mvc的路由是建立在asp.net核心的路由之上的.通过终结点加载路由中间件的配置方式在此不细说了,(DOTNET Core ...

  10. navicat连接异常 authentication plugin 'caching_sha2_password' 问题解决

    mysql 8.0 默认使用 caching_sha2_password 身份验证机制 -- 从原来的 mysql_native_password 更改为 caching_sha2_password. ...