ajax 请求后台

前端之家收集整理的这篇文章主要介绍了ajax 请求后台前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

 

先简单的记录一下 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;
	}
}
原文链接:https://www.f2er.com/ajax/166005.html

猜你在找的Ajax相关文章