我使用以下JavaScript / jQuery函数进行远程过程调用。
<script src="../js/jquery-1.8.0.min.js" type="text/javascript"></script>
<s:url var="testJsonUrl" action="testJsonAction"/>
var timeout;
var request;
$(document).ready(function(){
$("#btnUser").click(function(){
if(!request)
{
request = $.ajax({
datatype:"json",
type: "GET",
data: JSON.stringify({jsonrpc:'2.0', method:'getUser', id:'jsonrpc'}),
contentType: "application/json-rpc; charset=utf-8",
url: "<s:property value='#testJsonUrl'/>",
success: function(response)
{
var user = response.result;
alert(JSON.stringify(user)); //Always alerts "undefined".
},
complete: function()
{
timeout = request = null;
},
error: function(request, status, error)
{
if(status!=="timeout"&&status!=="abort")
{
alert(status+" : "+error);
}
}
});
timeout = setTimeout(function() {
if(request)
{
request.abort();
alert("The request has been timed out.");
}
}, 30000);
}
});
});
按下单击按钮时,将调用上述函数。
<s:form namespace="/admin_side" action="Test" validate="true" id="dataForm" name="dataForm">
<input type="button" name="btnUser" id="btnUser" value="Click"/>
</s:form>
要调用该方法的操作类如下所示。
import com.opensymphony.xwork2.ActionSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.json.annotations.SMDMethod;
import util.User;
@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value = "json-default")
public final class TestAction extends ActionSupport
{
private User user;
public TestAction() {}
@SMDMethod
public User getUser()
{
user = new User();
user.setName("Tiny");
user.setLocation("India");
try {
user.setDob(new SimpleDateFormat("dd-MMM-YYYY").parse("29-Feb-2000"));
} catch (ParseException ex) {
Logger.getLogger(TestAction.class.getName()).log(Level.SEVERE, null, ex);
}
return user;
}
public void setUser(User user) {
this.user = user;
}
@Action(value = "testJsonAction",
results = {
@Result(type = "json", params = {"enableSMD", "true", "excludeNullProperties", "true"})},
interceptorRefs = {
@InterceptorRef(value = "json", params = {"enableSMD", "true"})})
public String executeAction() throws Exception {
return SUCCESS;
}
@Action(value = "Test",
results = {
@Result(name = ActionSupport.SUCCESS, location = "Test.jsp"),
@Result(name = ActionSupport.INPUT, location = "Test.jsp")},
interceptorRefs = {
@InterceptorRef(value = "defaultStack", params = {"params.acceptParamNames", "", "params.excludeMethods", "load", "validation.validateAnnotatedMethodOnly", "true"})})
public String load() throws Exception {
//This method is just needed to return a view on page load.
return ActionSupport.SUCCESS;
}
}
User
类:
public class User
{
private String name;
private Date dob;
private String location;
//Setters & getters.
}
当点击给定按钮但未点击给定按钮时,预期会调用getUser()
方法(在动作类TestAction
中)注释@SMDMethod
。
jQuery函数的alert(JSON.stringify(user));
处理程序中的这一行success
始终会警告undefined
。
在地址栏中使用直接链接,例如
http://localhost:8080/TestStruts/admin_side/testJsonAction.action
返回以下字符串。
{
"methods": [{
"name": "getUser",
"parameters": []
}],
"serviceType": "JSON-RPC",
"serviceUrl": "\/TestStruts\/admin_side\/testJsonAction.action",
"version": ".1"
}
这里缺少什么?为什么JSON-RPC
无效?
PS:我正在使用Struts2-json-plugin-2.3.16
相同版本的框架。
答案 0 :(得分:1)
在使用JSON-RPC时是否必须只有POST请求?
是的,它只适用于POST请求
同样
includeProperties
无效
includeProperties
如果它是json
拦截器的参数,则会生效。不要将使用的参数名称与json
结果混淆。
在这种情况下,字段
dob
应该已被排除
在这种情况下,最好使用excludeProperties
参数来拦截,就像这样
@Action(value = "testJsonAction",
results = {
@Result(type = "json", params = {"enableSMD", "true"})},
interceptorRefs = {
@InterceptorRef(value = "json", params = {"enableSMD", "true", "excludeProperties", "result\\.dob"})})
public String executeAction() throws Exception {
return SUCCESS;
}