ajax跨页面提交表单_AJAX相关

前面提到过重复提交表单问题,处理token口令校验、重定向之外,还有一种经常使用到的方法就是新页面处理表单提交,完成后关闭当前页面,并刷新之前发送请求的页面。
这里使用了artDialog.js

1、文件结构

2、user.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>user列表</title> 

 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 <script type="text/javascript" src="/MySSH2/jquery-1.7.js"></script>
 <script type="text/javascript" src="/MySSH2/artDialog.js?skin=default"></script>
 <script type="text/javascript">
 function openA(){
 window.open("/MySSH2/user/manage_addUI");
 }
 </script>
 </head>
 <body>
 <br/>
 <a href="<s:url action="manage_addUI" namespace="/user"/>">添加用户</a>
 <a href="javascript:void(0)" onclick="openA()">添加用户</a>
  <br/>
  用户列表:<br/>
 <s:iterator value="#request.users">
 id:<s:property value="id"/><br/>
 name:<s:property value="name"/><br/>
 </s:iterator> 

 </body>
</html>

3、userAdd.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>用户添加</title> 

 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 <script type="text/javascript" src="/MySSH2/jquery-1.7.js"></script>
 <script type="text/javascript">
 function AddUser(){
 var submitData = $('#userForm').serialize();
 console.log(submitData);
 $.ajax({
    type : "post",
    url : "/MySSH2/user/manage_add",
    cache : false,
    data: submitData,
    dataType : 'json',
    success : function(result) {
    <span style="white-space:pre">  </span>if(result.success){
       window.opener.art.dialog({time:2,content:'保存成功'});
       setTimeout(function(){window.opener.location.reload();},3);
      }
      else{
      <span style="white-space:pre"> </span> window.opener.art.dialog({time:2,content:'保存失败'});
        setTimeout(function(){window.opener.location.reload();},3);
       }
       window.close();
      },
    error : function(XMLHttpRequest, textStatus,
      errorThrown) {
        alert("error");
      }
    });
 }
 </script>
 </head> 

 <body>
 <s:form id="userForm" action="manage_add" namespace="/user" method="post">
  用户名:<s:textfield name="user.name"/><br/><s:token></s:token>
  <input type="button" value="保存" onclick="AddUser()"/>
 </s:form>
 </body>
</html>

4、UserManageAction.java

package com.myssh2.action; 

import java.io.IOException;
import java.io.PrintWriter; 

import javax.annotation.Resource;
import javax.servlet.ServletException; 

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; 

import com.myssh2.bean.User;
import com.myssh2.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; 

@Controller @Scope("prototype")
public class UserManageAction extends ActionSupport{
 @Resource UserService userService;
 private User user; 

 public User getUser() {
  return user;
 } 

 public void setUser(User user) {
  this.user = user;
 } 

 public String addUI(){
  return "add";
 } 

 public void add() throws ServletException, IOException{ 

  ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
  PrintWriter out = ServletActionContext.getResponse().getWriter();
  try {
   userService.addUser(user);
   ActionContext.getContext().put("message", "保存成功");
   out.write("{\"success\":true}");
  } catch (Exception e) {
    e.printStackTrace();
    out.write("{\"success\":false,\"msg\":\"error\"}");
  }
 }
}

页面效果

提交表单时使用$('#userForm').serialize();序列化表单数据
 window.opener.art.dialog({time:2,content:'保存成功'});则是返回使用window.open的页面(或者理解为父页面),并调用artDialog插件的定时关闭dialog
setTimeout(function(){window.opener.location.reload();},3);使用定时器刷新使用window.open的页面(或者理解为父页面),dialog和reload的时间设置问题需重新调整。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索ajax提交form表单
ajax提交表单
ajax提交表单跳转页面、ajax提交form表单、ajax异步提交form表单、ajax提交表单、jquery ajax 提交表单,以便于您获取更多的相关知识。

时间: 2024-08-03 09:45:36

ajax跨页面提交表单_AJAX相关的相关文章

ajax跨页面提交表单

前面提到过重复提交表单问题,处理token口令校验.重定向之外,还有一种经常使用到的方法就是新页面处理表单提交,完成后关闭当前页面,并刷新之前发送请求的页面. 这里使用了artDialog.js 1.文件结构 2.user.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-ta

跨域提交表单问题,小白求助!

问题描述 跨域提交表单问题,小白求助! 1.html 代码 <form action="http://111.com/XXX.ASP" method="post"> <p>帐号: <input type="text" name="user" /></p> <p>密码: <input type="text" name="pass&qu

jquery下异步提交表单 异步跨域提交表单_jquery

1.使用post提交方式 2.构造表单的数格式 3.结合form表单的submit调用ajax的回调函数. 使用 jQuery 异步提交表单代码: 复制代码 代码如下: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>无标题页</title> </head> <script src="js/jquery-1.4.2.js">&l

jquery异步提交与跨域提交表单方法

2.采用jsonp跨域提交表单是比较好的解决方案. 3.也可以动态程序做一代理.用代理中转跨域请求   jquery(function($) { // 使用 jquery 异步提交表单 $('#f1').submit(function() { $.ajax({ url: 'ta.asp教程x', data: $('#f1').serialize(), type: "post", cache : false, success: function(data) {alert(data);}

ajax spring-ajax方式提交表单,spring mvc的Controller中怎么不能返回视图

问题描述 ajax方式提交表单,spring mvc的Controller中怎么不能返回视图 ajax请求到springmvc的Controller后,请求成功后为什么不跳转到jsp?就是这句:return new ModelAndView(""/sys/selectAccount"");不用ajax请求就可以 解决方案 使用ajax提交的请求 控制器中的重定向 或者请求转发都是无效的 ajax要跳转页面 只能把你要跳转的页面发送到前端 前端ajax的回调函数使用前

jQuery通过ajax快速批量提交表单数据_jquery

当表单数据项很多时,手动获取表单项的值将变得效率低下,结合jQuery提供的函数serialize(),我们可以实现快速获取数据并提交表单数据. 请看下面的表单: <form id="fm"> <table> <tr> <td>姓名</td> <td> <input type="text" name="name" /> </td> </tr>

jquery不刷新页面提交表单的例子

实例 通过 AJAX 加载一段文本: jQuery 代码:  代码如下 复制代码 $(document).ready(function(){   $("#b01").click(function(){   htmlobj=$.ajax({url:"/jquery/test1.txt",async:false});   $("#myDiv").html(htmlobj.responseText);   }); }); HTML 代码:  代码如下

Jquery ajax 简洁方式提交表单form

最简单就这样    代码如下 复制代码 $.post("/index.php?action=ajax&rs=TWProAjax::checkProTermsExisted", $(this).serialize(), function(data){ },'json'); 注意serialize就是把你表单所有input以post形式或get形式提交过去了,下面看个实例 我们有一个非常普通的表单: 首先,新建Login.html页面:  代码如下 复制代码 <!DOCTYP

零基础学习AJAX之制作自动校验的表单_AJAX相关

传统网页在注册时检测用户名是否被占用,传统的校验显然缓慢笨拙. 当ajax出现后,这种体验有了很大的改观,因为在用户填写表单时,签名的表单项已经发送给了服务器,然后根据用户填写好的内容进行数据查询.在查询号无需页面刷新就自动给了提示.类似这样的应用大大的提高了用户的体验,本节简单介绍自动校验表单制作方法.从原理上分析ajax的作用. 1.搭建框架 首先为html框架 复制代码 代码如下:     <form name="register">             <