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

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条回答 默认 最新

  • 已采纳

    标准浏览器下button是submit按钮,要设置类型为button,要不会提交表单导致ajax根本没执行。。而且你jquery3+,jquery2+不在支持ie8-,注意jquery版本

     <button>登录</button>
    ==>
    <button type="button">登录</button>
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题