GVKun编程网logo

ajax接收action的数据返回一大串的html代码的问题(ajax接收返回的字符串)

22

针对ajax接收action的数据返回一大串的html代码的问题和ajax接收返回的字符串这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展$.ajax接收.xml文件、@Ajax.Actio

针对ajax接收action的数据返回一大串的html代码的问题ajax接收返回的字符串这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展$.ajax接收.xml文件、@Ajax.ActionLink跳转页面的问题解决方案 MVC Ajax不支持问题、ajax 中接收不到 action 返回的 data 数据、ajax 同步异步设置导致的问题及 return 返回值(返回 ajax 请求的数据)等相关知识,希望可以帮助到你。

本文目录一览:

ajax接收action的数据返回一大串的html代码的问题(ajax接收返回的字符串)

ajax接收action的数据返回一大串的html代码的问题(ajax接收返回的字符串)

需要在out.println("xxxx")后加入如下的代码 out.close()

package com.zb.web.action;

import java.io.PrintWriter;
import java.util.Map;

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

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zb.domain.Employee;
import com.zb.service.interfac.EmployeeServiceInter;

public class LoginAction extends ActionSupport {
	
	
	private String id;
	private String password;
	private Map session  = ActionContext.getContext().getSession(); ;  
	private HttpServletResponse response = ServletActionContext.getResponse();
	private HttpServletRequest request = ServletActionContext.getRequest();
	private EmployeeServiceInter employeeService;
	
	//定义计数器,测试是否是一个sessionfactory
	private int a;

	
	
	public EmployeeServiceInter getEmployeeService() {
		return employeeService;
	}
	@Resource
	public void setEmployeeService(EmployeeServiceInter employeeService) {
		this.employeeService = employeeService;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getpassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
public String checkEmp() throws Exception {
	//ͨ������������spring��ʵ��˷�������
//	 WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
           //��spring�л�ȡbean
	//	  EmployeeServiceInter employeeServiceInter= (EmployeeServiceInter) ctx.getBean("employeeService");
	  
	//��a��ֵ�Ƿ�仯��֤ÿ������ʱaction�Ƿ���һ���������һ���Ļ���a��ֵ�������ӣ�����ǣ���aһֱ��0
	    //����һ��employee����
	//System.out.println("a="+(++a)); 
     if(request.getParameter("password")!=null&&request.getParameter("id")!=null) {
    	 id=request.getParameter("id");
    	 password=request.getParameter("password");
    	 
     }
	response.setCharacterEncoding("utf-8");
	PrintWriter out = response.getWriter();
	System.out.println(id);
	System.out.println(password);
	 Employee e = new Employee();
	 e.setId(Integer.parseInt(id));
	 e.setPassword(password);
	  e = employeeService.checkEmployee(e);
	  if(e!=null){
		  //将登陆的用户信心放到session中
		  
		  session.put("loginUser",e);
		  if(e.getGrade()==5){
		  return "admin";
		}else{
			
			return "putong";
		}
	  }else{
		  
	       out.println("用户名或者密码错误");
	       out.close();
			 return INPUT;
	  }
	
	/*if("1".equals(id)&&"123".equals(password)){
			return SUCCESS;
			
		}else{
			 this.addFieldError("flag","id���������������������");
			 return INPUT;
		}*/
		
	}

//注销用户
	public String cancellationEmp(){
		session.clear();
	return SUCCESS;
		
	}

}  
登录界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>

<%
String path = request.getcontextpath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>
  <head>
   <Meta http-equiv="pragma" content="no-cache">
	<Meta http-equiv="cache-control" content="no-cache">
	<Meta http-equiv="expires" content="0">    
	<Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<Meta http-equiv="description" content="This is my page">
  
   <title>用户登录界面</title>
     <style type="text/css">
     	.login{
     		font-family: Arial;
     		font-size: 14px;
     	}
     	
     	.newStyle {
			opacity: 0.5;
			Box-shadow: 5px 5px 5px rgba(0,0.5) ;
		    padding:10px; width:300px; height:50px;
		    border: 5px solid #dedede;
		    -moz-border-radius: 15px;     
		    border-radius:15px;          
		    background: -moz-radial-gradient(30px 30px,circle farthest-corner,#58ff00 0%,rgba(222,255,0) 30%);   
		    vertical-align: middle;
}
     </style>
  <script type="text/javascript" src="/myFirstSSH03/js/myjs2.js"></script>
  </head>
  <body topmargin="150px">
  <center>
        <p></p>
    	<form action="check_loginAction" method="post">
    	   <fieldset id="inputs"><br>
    	      <legend>雇员登录</legend>
    id:<input type="text"id="name" name="id" placeholder="请输入id" > <br><br>
              
         密码:<input type="password"name="password" id="pwd" placeholder="请输入密码" ><div id="div1"></div><br><br>
    
   </fieldset>
              <input type="submit" value="登录">
              <input type="reset" value="重置">
    	</form>
 </center>
  </body>
</html>

myjs2.js
function ajaxFunction()
 {
   var xmlHttp;
 
 try{
   // Firefox,Opera 8.0+,Safari
    xmlHttp=new XMLHttpRequest();
 }catch (e){

  // Internet Explorer
   try{
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e){
    try {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
       }catch (e){
         alert("您的浏览器不支持AJAX!");
         return false;
      }
    }
  }
	return xmlHttp;
 }
window.onload=function(){
	//<th width="100px"><input type="text" id="name"  name="name" onblur="demo()"/></th>
	
	document.getElementById("pwd").onblur=function(){
		var xht= ajaxFunction();
		
		//接收从服务器传回来的信息
		xht.onreadystatechange=function(){
			if(xht.readyState==4&&xht.status==200){
				var div1Element=document.getElementById("div1");
				  var data=xht.responseText;
				 // alert(flag.equals("wrong"));
				  alert(data);
				   	div1Element.innerHTML = "<font color='red'>"+data+"</font>";
				  
			
			}
			
		}
		 var id=document.getElementById("name").value;
		 var password=document.getElementById("pwd").value;
		//连接服务器
		xht.open("get","./check_loginAction?timestatus="+new Date().getTime()+"&id="+id+"&password="+password,true);
		//发送数据
		xht.send(null);
		
	}
	 
	
	
	
}

$.ajax接收.xml文件

$.ajax接收.xml文件

  $.ajax({} )接收的.xml文件在前段怎解析  现在已经能看到.xml文件里的内容了  就是不知道怎么解析  求好心人帮助  感激不尽

@Ajax.ActionLink跳转页面的问题解决方案 MVC Ajax不支持问题

@Ajax.ActionLink跳转页面的问题解决方案 MVC Ajax不支持问题

【JavaScriptResult】在客户端执行服务器返回的JavaScript代码
当一个内置的Ajax辅助方法请求一个操作方法,该方法会返回一个在客户端执行立即的脚本。

public ActionResult DoSomething()
{
     string s = "$(''#some-div'').html(''Updated'')";
     return JavaScript(s);
}

 

@Ajax.ActionLink("单击", "DoSomething", new AjaxOptions());
<div id="some-div"></div>

 

使用JavaScript的视图页面需要引用以下js,请使用NuGet安装:

@section Scripts{
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
}

貌似使用Ajax.ActionLink跳转了页面,而不是刷新当前视图。最后通过分析发现视图代码、脚本引用均未出现问题。而是web.config中缺少以下配置:
在appSettings节点下需要启用对JavaScript的支持,否则也是无法使用@Ajax.ActionLink的。

<appSettings>
<!--<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />-->
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

 

ajax 中接收不到 action 返回的 data 数据

ajax 中接收不到 action 返回的 data 数据

各位大大们,小弟现在要用 ajax 传几个参数给后台方法中去进行查询数据然后在 jsp 中显示,但是现在的问题是后台方法中可以接收到前台传过来的参数值,可是 ajax 中确接收不到后台返回的 data(后台方法和数据库都是可用的,是可以查到数据的),下面是代码

ajax 同步异步设置导致的问题及 return 返回值(返回 ajax 请求的数据)

ajax 同步异步设置导致的问题及 return 返回值(返回 ajax 请求的数据)

一、ajax 同步异步设置导致父子页面数据更新不一致问题。

  1、在修复入宿退宿功能时,发现点击退宿、入宿按钮时。父页面数据更新了但是子页面数据要重新打开才能更新。如下图:

    

    因为数据刷新函数如下:父页面刷新函数 findRoomPeople () 采用的是异步,所以,操作的数据还没更新,子页面刷新函数 loadRoomInfo () 就已经执行,所以获取到的数据还是之前的数据。

    

<script>
    roomHandle: function () {
        /*刷新数据变化*/
        parent.cootoo_room.findRoomPeople();
        addPeople.loadRoomInfo();
        addPeople.currentPeopleID = {};
    },
</script>

 

    修改为同步之后,子页面函数就能及时获取请求后的数据了。

二、return 返回值返回 ajax 请求的数据。

  1、详情可见 https://blog.csdn.net/zyw23zyw23/article/details/77775730;

  2、大致就是设置一个全局变量,然后请求之后将数据赋值给全局变量,return 的时候返回该全局变量。需要注意的是,return 返回的不是 ajax 里面的函数返回值,而应该是整个函数的返回值。

 

今天关于ajax接收action的数据返回一大串的html代码的问题ajax接收返回的字符串的分享就到这里,希望大家有所收获,若想了解更多关于$.ajax接收.xml文件、@Ajax.ActionLink跳转页面的问题解决方案 MVC Ajax不支持问题、ajax 中接收不到 action 返回的 data 数据、ajax 同步异步设置导致的问题及 return 返回值(返回 ajax 请求的数据)等相关知识,可以在本站进行查询。

本文标签: