和二不要调皮 2016-07-30 11:45 采纳率: 42.9%
浏览 1446
已采纳

jquery.ajax struts2 不能返回数据

 jquery.ajax struts2 采用上下文方式获取response, 提交两次才能提交到struts里,而且不能返回,偶尔能返回,这是在IE上的,在firefox不能访问,代码如下:
前端代码:
<script src="jquery-3.0.0.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){

var username= document.getElementById("name").value;
var password = document.getElementById("password").value;
alert(username+" "+password);

//var json = {"name":username,"password":password};
$.ajax({
type:"get",
url:"http://127.0.0.1:8080/AjaxJsonStruts1/login.action",
async:true,
//data:"json="+JSON.stringify(json),
data:"name="+username+"&password="+password,
success:function(datas){
alert(datas);

},
error:function(){

alert("登录出错");
//window.open("error.html");
}
});


});

});


</script>
</head>
<body>
<form>
用户名:<input type="text" id="name" /><br />
密码:    <input type="password" id="password"/><br />
<button>登录</button><br />
<!--<button id="register">注册</button>-->
</form>


后台:

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
<constant name="struts.devMode" value="true"/>
 <constant name="struts.i18n.encoding" value="UTF-8" />
<package name="default"  extends="struts-default" >
<action name="login" class="com.action.LoginAction" method="login"/>
<action name="register" class="com.action.RegisterAction"/>
</package>
</struts>


package com.action;

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




import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.bean.User;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.xml.xsom.impl.WildcardImpl.Other;

public class LoginAction extends ActionSupport{

private HttpServletRequest request;
private HttpServletResponse response;
public LoginAction(){
request = ServletActionContext.getRequest();
response = ServletActionContext.getResponse();
}
public void login() throws IOException{
//System.out.println("begin");
//response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Cache-Control", "no-cache");

response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
/*String jsonstring = request.getParameter("json");
JSONObject json = JSONObject.fromObject(jsonstring);*/
    User user = new User();
    String name = request.getParameter("name");
    String password = request.getParameter("password");
    user.setName(name);
    user.setPassword(password);
    //user = (User) JSONObject.toBean(json, User.class);
System.out.println("user :  "+user.getName()+"  : "+user.getPassword());
String answer="";
if("admin".equals(user.getName())&&"123456".equals(user.getPassword())){
System.out.println("success");
answer="success";

}else{
System.out.println("erorrr");
answer="error";
}
out.print(answer);
out.flush();
out.close();
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
}
  • 写回答

1条回答 默认 最新

    报告相同问题?

    相关推荐 更多相似问题

    悬赏问题

    • ¥20 关于#pandas#的问题,如何解决?(语言-python)
    • ¥15 eclipse为什么导入不了RAR阿
    • ¥30 c语言设计井字棋小游戏
    • ¥15 关于属性关联的双极容度多属性决策vikor方法的公式
    • ¥15 10吨190马力爆炸品运输车,以80km/h的速度行驶1个小时输出的动能或者做的功是多少?
    • ¥15 《phthon编程从入门到实践》第十八章,从django入手,learning_log,ModuleNotFoundError: No module named 'learning_logs
    • ¥15 echarts 放入折叠面板宽高的问题
    • ¥50 求帮配置Django项目配置环境
    • ¥20 画出下列系统的bode图和乃式图
    • ¥15 在matplotlib作图合理规划日期网格线柱状线的关系