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. Codeforces Round #600 (Div. 2) E. Antenna Coverage

    Codeforces Round #600 (Div. 2) E. Antenna Coverage(dp) 题目链接 题意: m个Antenna,每个Antenna的位置是\(x_i\),分数是\( ...

  2. ajax jsonp跨域 【转】

    跨域的基本原理:    JSONP跨域GET请求是一个常用的解决方案,    JSONP的最基本的原理是:动态添加一个<script>标签,而script标签的src属性是没有跨域的限制的 ...

  3. 必知必会之Lambda表达式

    Java是一门强大的面向对象的语言,除了8种基本的数据类型,其他一切皆为对象.因此,在Java中定义函数或方法都离不开对象,也就意味着很难直接将方法或函数像参数一样传递,而Java8中的Lambda表 ...

  4. Yandex Big Data Essentials Week1 Unix Command Line Interface File Content exploration

    cat displays the contents of a file at the command line copies or apppend text file into a document ...

  5. 15-Git使用语法

    Git命令 版本库的创建 方法一:使用git bash 1. 在当当前要加创建版本库的文件夹右键使用GitBash 创建仓库执行命令: $ git init 方法二:使用TortoiseGit 2. ...

  6. iptbales 允许访问vsftp

    1.允许20 21 端口iptables -I INPUT -p tcp -m multiport --dport 20,21 -j ACCEPT 2.允许关联包通过iptables -A INPUT ...

  7. Fastdfs php扩展访问

    一.安装FastDFS client php extension compiled under PHP 5.4 and PHP 7.0   1.安装php扩展,进入fastdfs源码文件夹中的  ph ...

  8. centos7上安装docker社区版

    container(容器) docker(集装箱) 容器的优点 1. 启动速度快 2. 节省资源 3. 兼容性高 保证机器正常上网 #ping www.baidu.com CPU需要支持虚拟化 # g ...

  9. thinkPHP中Model的字段映射问题

    在model定义中不要自己定义相应的字段变量,因为一旦定义,之后的赋值会直接赋给自己定义的属性,但实际上model抽象类重写了__set()方法,将未定义的属性存入了$data里面,写入数据库是也会从 ...

  10. C#设计模式学习笔记:(18)状态模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/8032683.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第六个模式--状 ...