加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax 请求后台

发布时间:2020-12-16 00:56:57 所属栏目:百科 来源:网络整理
导读:先简单的记录一下 ajax 的用法 HttpServletResponse response = ServletActionContext.getResponse();response.setCharacterEncoding("utf-8");PrintWriter out; 前台 : $.ajax({ url:'%=basePath%AutoCodeSession',//这里是你的action或者servlert的路径地

 

先简单的记录一下 ajax 的用法

HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
PrintWriter out;


前台 :

$.ajax({ 
			url:'<%=basePath%>AutoCodeSession',//这里是你的action或者servlert的路径地址 
			type:'post',//数据发送方式 
			dataType:'json',//接受数据格式 (这里有很多,常用的有html,xml,js,json) 
			//data:'',//要传递的数据 
			data:'username='+$("#username").val()+'&password='+$("#password").val(),//要传递的数据 
			error: function(msg){ //失败 
			alert('Error loading document');
		 	alert(msg.responseText);
			},success: function(msg){ //成功 
	                alert('username='+$("#username").val()+'&password='+$("#password").val());
 			alert(msg.url);//根据后台返回值前台可以获取值
			}
			}); 	 

后台:
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {

		System.out.println("dopost------");
		response.setContentType("text ml;charset=utf-8");  
		// 设置浏览器禁用缓存
		response.setHeader("pragma","no-cache");
		response.setHeader("cache-control","no-cache");
		HttpSession session = request.getSession(true);
		String orgHref = (String) session.getAttribute("orgHref");
		System.out.println("wangzhi :====="+orgHref);
 		String jsonString = "{"success":"true","url":""+orgHref+""}";//这里设定 返回到前台的值  前台可以用 msg.url来获取
 		String userName = request.getParameter("username");
		String passWord = request.getParameter("password");

		System.out.println("suername ----"+userName);
 		PrintWriter out;
		try {
		out = response.getWriter();
		out.print(jsonString);
		out.flush();
		out.close();
		} catch (IOException e) {
		e.printStackTrace();
		}
	}
	public void doGet(HttpServletRequest request,IOException {
		this.doPost(request,response);
	}

后台 struts


package cn.lin.action.check;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.context.ApplicationContext;

import com.opensymphony.xwork2.ActionContext;

import cn.fulong.common.web.action.BaseAction;
import cn.fulong.growth.entity.GrowthDmApplyInfo;
import cn.fulong.growth.service.DirecApplyService;

/**
 *  这里写的是一个上传文件的验证
 *
 */
public class DirecapplyAction  extends BaseAction{
	

	 
	/**
	 * 页面提示信息
	 */
	private String msg;
	 
	
	 
	private File files;
	
	 
	private String filesFileName;
	
	private String filesContentType ;
	
	
	 
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	 
	public File getFiles() {
		return files;
	}
	public void setFiles(File files) {
		this.files = files;
	}
	public String getFilesFileName() {
		return filesFileName;
	}
	public void setFilesFileName(String filesFileName) {
		this.filesFileName = filesFileName;
	}
	public String getFilesContentType() {
		return filesContentType;
	}
	public void setFilesContentType(String filesContentType) {
		this.filesContentType = filesContentType;
	}
	 
	public String checkFile() throws IOException{
		if(null == files){
			/**若文件为空返回""**/
			msg= "宣传材料不能为空";
		}else{
			// 判断数据是否正确
			// 文件后缀名
			int index = StringUtils.lastIndexOf(filesFileName,'.');
			if (index == -1) {
				msg= "文件类型错误!";
			}
			/**获取文件后缀名**/
			String extFileName = StringUtils.substring(filesFileName,index + 1);
			String forbitName = "doc-docx";
			String[] forbitNames = forbitName.split("-");
			for(int i= 0 ;i<forbitNames.length ; i++){
				 if(extFileName.equalsIgnoreCase(forbitNames[i])){
					 msg= "请上传正确的附件,附件不能类型不能为"+forbitNames[i];
					 break;
				 }
			 }
			
		}
		HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);  
		response.getWriter().println(new String (msg.getBytes("ISO8859_1"),"utf-8"));//利用这样的写法 来获取后台返回值
		return null;
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读