问题精简如下:
javascript中:function login(){
var url="Login_login";
var params={"id":1,"password":"110"};
//ajax()方法
jQuery.ajax({
type: "get",
async: true,
url: url,
data: params,
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (data) {
alert("success "+data.id);
},
error: function (err) {
alert("erro");
}
});struts.xml如下: <package name="default" namespace="/" extends="json-default">
<action name="*_*" class="com.sanxiau.action.{1}Action"
method="{2}">
<result type="json" name="success">
<param name="incluepropeties">
result
</param>
</result>
</action>
</package>
action如下:public class UserLoginAction {
private int id;
private String password;
private boolean isSuccess;
//不用new,用spring容器提供
//private UserService userService=new UserServiceImp();过时
private UserService userService;
private ApplicationContext applicationContext=null;
private String result;
public String login(){
/*测试一下*/
System.out.println("id="+this.getId()+"password="+this.getPassword()); // applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// userService = (UserService) applicationContext.getBean("UserServiceImpl");
// isSuccess=userService.checkIdAndPassword(id, password);
Map<String, Comparable> map = new HashMap<String, Comparable>();
map.put("id", this.getId());
map.put("password",this.getPassword());
JSONObject obj = JSONObject.fromObject(map);
result = obj.toString();
System.out.println("result is "+result);
return "success";
}
当打开action中注释的那三排就返回执行的就是alert("erro");
不管async: true,还是false!很纠结啊!!
为什么会这样呢,我用firefox调试看到,打开action注释的那三排执行时间就要3s左右,因为访问了数据库,时间耗得多嘛,然而注释掉就600ms左右,难道这跟action中执行的时间有关,
action中测试的均正确,控制台输出的都是json格式的;AjaxSSH登陆验证
javascript中:function login(){
var url="Login_login";
var params={"id":1,"password":"110"};
//ajax()方法
jQuery.ajax({
type: "get",
async: true,
url: url,
data: params,
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (data) {
alert("success "+data.id);
},
error: function (err) {
alert("erro");
}
});struts.xml如下: <package name="default" namespace="/" extends="json-default">
<action name="*_*" class="com.sanxiau.action.{1}Action"
method="{2}">
<result type="json" name="success">
<param name="incluepropeties">
result
</param>
</result>
</action>
</package>
action如下:public class UserLoginAction {
private int id;
private String password;
private boolean isSuccess;
//不用new,用spring容器提供
//private UserService userService=new UserServiceImp();过时
private UserService userService;
private ApplicationContext applicationContext=null;
private String result;
public String login(){
/*测试一下*/
System.out.println("id="+this.getId()+"password="+this.getPassword()); // applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// userService = (UserService) applicationContext.getBean("UserServiceImpl");
// isSuccess=userService.checkIdAndPassword(id, password);
Map<String, Comparable> map = new HashMap<String, Comparable>();
map.put("id", this.getId());
map.put("password",this.getPassword());
JSONObject obj = JSONObject.fromObject(map);
result = obj.toString();
System.out.println("result is "+result);
return "success";
}
当打开action中注释的那三排就返回执行的就是alert("erro");
不管async: true,还是false!很纠结啊!!
为什么会这样呢,我用firefox调试看到,打开action注释的那三排执行时间就要3s左右,因为访问了数据库,时间耗得多嘛,然而注释掉就600ms左右,难道这跟action中执行的时间有关,
action中测试的均正确,控制台输出的都是json格式的;AjaxSSH登陆验证
这样可以吗?[HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");// 防止弹出的信息出现乱码
PrintWriter writer = response.getWriter();
String json= "{exist:\""+data+"\"}";
writer.write(json);
嗯,不需要配置xml的json,直接就是response这个字符出去,然后return none就可以了;页面的ajax 回调function中的data就是这串字符,然后你使用eval方法这字符编程json对象,然后按json对象来使用就可以了
我现在的代码是这样的,
javascript中jQuery.ajax({
type: "get",
timeout:0,
async: false,
url: url,
data: params,
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (response) {
// alert(response.id);
$("#userShow").html(response.id);
},
error: function (response) {
alert(response.statusText);
}
});
Struts.xml中 <action name="*_*" class="com.sanxiau.action.{1}Action"
method="{2}">
</action>action中:public String login(){
/*测试一下*/
System.out.println("id="+this.getId()+"password="+this.getPassword());
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
userService = (UserService) applicationContext.getBean("UserServiceImpl");
isSuccess=userService.checkIdAndPassword(id, password); HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");// 防止弹出的信息出现乱码
PrintWriter writer;
try {
writer = response.getWriter();
String json= "{id:\""+id+"\"}";
writer.write(json);
System.out.println(json);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return NONE;
}
结果执行的是alert(response.statusText);,而且response.statusText为OK
但是同样不执行访问数据库那一段操作的话,就成功了!
我现在的代码是这样的,
javascript中jQuery.ajax({
type: "get",
timeout:0,
async: false,
url: url,
data: params,
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (response) {
// alert(response.id);
$("#userShow").html(response.id);
},
error: function (response) {
alert(response.statusText);
}
});
Struts.xml中 <action name="*_*" class="com.sanxiau.action.{1}Action"
method="{2}">
</action>action中:public String login(){
/*测试一下*/
System.out.println("id="+this.getId()+"password="+this.getPassword());
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
userService = (UserService) applicationContext.getBean("UserServiceImpl");
isSuccess=userService.checkIdAndPassword(id, password); HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");// 防止弹出的信息出现乱码
PrintWriter writer;
try {
writer = response.getWriter();
String json= "{id:\""+id+"\"}";
writer.write(json);
System.out.println(json);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return NONE;
}
结果执行的是alert(response.statusText);,而且response.statusText为OK
但是同样不执行访问数据库那一段操作的话,就成功了!那很明显是你的数据库操作返回有问题吧...你输出下,而且你直接response数据出来是不能用data.js这样引用的,因为他本来就是一个字符串而已,你只有使用了eval成json对象才能这样使用
输出的是{id:"1"};
而且那个eval就算是错的也没关系啊,它都不执行succes的操作,直接执行了error操作
输出的是{id:"1"};
而且那个eval就算是错的也没关系啊,它都不执行succes的操作,直接执行了error操作那你直接用url访问这个action的login方法,看看有木有弹出新页面的数据
那不就可以了..很明显是你的ajax写法有点问题吧.用这简化版的$.post比较方便
开始我用$.getjson();后来也用$.ajax();怎么都不行,$.post就行了,jQuery.ajax({
type: "post",
timeout:100000,
async: true,
url: url,
data: params,
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (response) {
alert(response);
// $("#userShow").html(result.id);
},
error: function (response) {
alert(response.statusText);
}
});与
$.post(url,params,function(data){var result= eval("("+data+")");alert(result.id);});貌似差不多吧,$.post不就是$.ajax其中的一个精简版吗?