5 个解决方案
#1
你后台response向前台传的东西 在回调里function(data)中用data.responseText取
#2
把代码发上来吧 ,更直观一些。
#3
js代码:
var req=false;
function create(){
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function getcheckcode(){
create();
// var url = "/springtestone/Getcheckcode?p=p1";
var url = "getcode.action";
req.open("GET",url, true);
req.send(null);
req.onreadystatechange = complete;
}
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
var json=req.responseText;
document.getElementById("checkcode").value=json;
}
}
}
struts.xml文件
<action name="getcode"
class="serv.Getcheckcode2">
<result name="success">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
Getcheckcode2.java:
package serv;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Getcheckcode2 extends ActionSupport{
public String execute() throws Exception{
combo cb=new combo();
String checkcode=cb.makecheckcode();
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("checkcode", checkcode);
return "success";
}
}
combo.java//获取验证码
package serv;
import java.util.Random;
public class combo {
public void combo(){};
public String makecheckcode(){
char[] c={'a','b','c','d','e','f','g','h','j','k','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','1','2','3','4','5','6','7','8','9'};
StringBuffer bf=new StringBuffer();
Random ran=new Random();
// char[] code=null;
for(int i=0;i<4;i++){
int a=ran.nextInt(c.length);
bf.append(c[a]);
}
System.out.println(bf.toString());
return bf.toString();
}
}
var req=false;
function create(){
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function getcheckcode(){
create();
// var url = "/springtestone/Getcheckcode?p=p1";
var url = "getcode.action";
req.open("GET",url, true);
req.send(null);
req.onreadystatechange = complete;
}
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
var json=req.responseText;
document.getElementById("checkcode").value=json;
}
}
}
struts.xml文件
<action name="getcode"
class="serv.Getcheckcode2">
<result name="success">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
Getcheckcode2.java:
package serv;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Getcheckcode2 extends ActionSupport{
public String execute() throws Exception{
combo cb=new combo();
String checkcode=cb.makecheckcode();
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("checkcode", checkcode);
return "success";
}
}
combo.java//获取验证码
package serv;
import java.util.Random;
public class combo {
public void combo(){};
public String makecheckcode(){
char[] c={'a','b','c','d','e','f','g','h','j','k','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','1','2','3','4','5','6','7','8','9'};
StringBuffer bf=new StringBuffer();
Random ran=new Random();
// char[] code=null;
for(int i=0;i<4;i++){
int a=ran.nextInt(c.length);
bf.append(c[a]);
}
System.out.println(bf.toString());
return bf.toString();
}
}
#4
PrintWriter out = response.getWriter();
out.print(里面写你要返回的字符串);
你action返回字符串写法有问题,用我上面这个返回字符串
out.print(里面写你要返回的字符串);
你action返回字符串写法有问题,用我上面这个返回字符串
#5
4楼的方法赞同,我也经常这样用。
#1
你后台response向前台传的东西 在回调里function(data)中用data.responseText取
#2
把代码发上来吧 ,更直观一些。
#3
js代码:
var req=false;
function create(){
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function getcheckcode(){
create();
// var url = "/springtestone/Getcheckcode?p=p1";
var url = "getcode.action";
req.open("GET",url, true);
req.send(null);
req.onreadystatechange = complete;
}
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
var json=req.responseText;
document.getElementById("checkcode").value=json;
}
}
}
struts.xml文件
<action name="getcode"
class="serv.Getcheckcode2">
<result name="success">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
Getcheckcode2.java:
package serv;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Getcheckcode2 extends ActionSupport{
public String execute() throws Exception{
combo cb=new combo();
String checkcode=cb.makecheckcode();
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("checkcode", checkcode);
return "success";
}
}
combo.java//获取验证码
package serv;
import java.util.Random;
public class combo {
public void combo(){};
public String makecheckcode(){
char[] c={'a','b','c','d','e','f','g','h','j','k','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','1','2','3','4','5','6','7','8','9'};
StringBuffer bf=new StringBuffer();
Random ran=new Random();
// char[] code=null;
for(int i=0;i<4;i++){
int a=ran.nextInt(c.length);
bf.append(c[a]);
}
System.out.println(bf.toString());
return bf.toString();
}
}
var req=false;
function create(){
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function getcheckcode(){
create();
// var url = "/springtestone/Getcheckcode?p=p1";
var url = "getcode.action";
req.open("GET",url, true);
req.send(null);
req.onreadystatechange = complete;
}
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
var json=req.responseText;
document.getElementById("checkcode").value=json;
}
}
}
struts.xml文件
<action name="getcode"
class="serv.Getcheckcode2">
<result name="success">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
Getcheckcode2.java:
package serv;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Getcheckcode2 extends ActionSupport{
public String execute() throws Exception{
combo cb=new combo();
String checkcode=cb.makecheckcode();
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("checkcode", checkcode);
return "success";
}
}
combo.java//获取验证码
package serv;
import java.util.Random;
public class combo {
public void combo(){};
public String makecheckcode(){
char[] c={'a','b','c','d','e','f','g','h','j','k','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','1','2','3','4','5','6','7','8','9'};
StringBuffer bf=new StringBuffer();
Random ran=new Random();
// char[] code=null;
for(int i=0;i<4;i++){
int a=ran.nextInt(c.length);
bf.append(c[a]);
}
System.out.println(bf.toString());
return bf.toString();
}
}
#4
PrintWriter out = response.getWriter();
out.print(里面写你要返回的字符串);
你action返回字符串写法有问题,用我上面这个返回字符串
out.print(里面写你要返回的字符串);
你action返回字符串写法有问题,用我上面这个返回字符串
#5
4楼的方法赞同,我也经常这样用。