问题描述
[已解决]操作名称竟然区分大小写
[SOLVED] Name of action turned out to be case sensitive
我想向服务器发出ajax请求以获取JSON.我正在使用以这种方式配置的struts2软件包
I want to do an ajax request to server in order to get a JSON. I am using struts2 package configured in this way
<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类是
GetDayAction class is
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帖子块是
jQuery post block is
$(function() {
$.post('getday.action', document.getElementById('startDate').innerHTML,
function(data) {
});
});
在获取jQuery块的情况下
in case of get jQuery block is
$(function() {
$.getJSON('getday.action', document.getElementById('startDate').innerHTML,
function(data) {
});
});
document.getElementById('startDate').innerHTML
获取以字符串格式设置的日期.
document.getElementById('startDate').innerHTML
gets a Date formatted in a string.
Tomcat通过ajax执行get或post时,Tomcat给了我500个内部服务器错误和此堆栈跟踪
When it executes get or post via ajax, Tomcat gives me 500 internal server error and this stacktrace
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()或其他消息均不显示.
Tomcat7 console doesn't show up anything, neither System.out.println() nor other messages.
我不知道自己在做什么错.谢谢
I have no clue about what I am doing wrong.Thanks
推荐答案
$.post()/$.getJSON()
函数的data
参数必须是一个对象或URL参数字符串.您不能只将其传递一个值并期望它知道如何处理它.因此,要么:
The data
parameter of the $.post()/$.getJSON()
functions either has to be an object, or a URL parameter string; you can't just pass it a single value and expect it to know what to do with it. So, either:
$.getJSON('getday.action', { yourparam : document.getElementById('startDate').innerHTML},
function(data) {
});
或
$.getJSON('getday.action', 'yourparam=' + document.getElementById('startDate').innerHTML},
function(data) {
});
这篇关于尝试在jQuery + Struts2中执行ajax getJson或post操作时,Tomcat7上出现500个内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!