ajax调用action
ajax调用action时,action怎么配置的?还有action里的result标签有什么用,action能返回json吗?能给一个具体的例子么?
[解决办法]
struts.xml中:
<package>
<!--在这里面配置action-->
<action name="名" class="路径" >
<result name="result" type="json">跳转的页面</result>
</action>
</package>
action:
public String login(){
//数据逻辑处理
return "result";
}
jsp:
$.getJSON("action",{参数},function(data){
var jsonData= val(data);
});
[解决办法]
Js
$.ajax({
type: "POST",
url: "/xxx/ReturnJson",
data: "name=John&location=Boston",
dataType:"json",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
public ActionResult ReturnJson()
{
return "json字符串";
}
$.ajax({
type: "POST",
url : "videoSec_findBySid.action",
data: "sid="+sid ,
success: function(msg){
var data = eval(msg) ;
},
async:false
});
public void findBySid()throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String sid = request.getParameter("sid") ;
VideoSec vs = videoSecService.findBySid(Integer.parseInt(sid)) ;
String jsonData = "["+toJson(vs)+"]" ;
out.print(jsonData) ; //json数据传给前台
out.flush() ;
out.close() ;
}