[已解决]操作名称竟然区分大小写
我想向服务器发出ajax请求以获取JSON。我正在使用以这种方式配置的struts2软件包
<package name="it.polito.ai.e3" namespace="" extends="struts-default,json-default"> <action name="getDay" class="it.polito.ai.e3.GetDayAction" > <result name="success" type="json" /> <result name="error" type="json" /> </action> </package>
GetDayAction类为
package it.polito.ai.e3; import java.util.Date; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class GetDayAction extends ActionSupport { private String startDate; public String execute() { try { System.out.println("Ciao"); System.out.println(startDate); } catch (Exception e) { e.printStackTrace(); return Action.ERROR; } return Action.SUCCESS; } public String getStartDate() { return startDate; } public void setStartDate(String startDate) { this.startDate = startDate; } }
jQuery post块是
$(function() { $.post('getday.action', document.getElementById('startDate').innerHTML, function(data) { }); });
在获取jQuery块的情况下
$(function() { $.getJSON('getday.action', document.getElementById('startDate').innerHTML, function(data) { }); });
document.getElementById('startDate').innerHTML 获取以字符串格式设置的日期。
document.getElementById('startDate').innerHTML
当它通过ajax执行get或post时,Tomcat给我500个内部服务器错误,并且此堆栈跟踪
java.lang.NullPointerException org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69) com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185) org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63) org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39) com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58) org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:500) org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
Tomcat7控制台不显示任何内容,System.out.println()或其他消息均不显示。
我不知道我在做什么错。谢谢
函数的data参数$.post()/$.getJSON()要么必须是对象,要么是URL参数字符串。您不能只将其传递一个值,并期望它知道如何处理它。因此,要么:
data
$.post()/$.getJSON()
$.getJSON('getday.action', { yourparam : document.getElementById('startDate').innerHTML}, function(data) { });
要么
$.getJSON('getday.action', 'yourparam=' + document.getElementById('startDate').innerHTML}, function(data) { });