我没有收到来自服务器的 JSON 类型数据的响应。
我正在使用 JSON 插件。
jQuery( "#dialog-form" ).dialog({
autoOpen: false,
height: 500,
width: 750,
modal: true,
buttons :{
"Search" : function(){
jQuery.ajax({type : 'POST',
dataType : 'json',
url : '<s:url action="part" method="finder" />',
success : handledata})
}
}
});
var handledata = function(data)
{
alert(data);
}
如果dataType = 'json'
我没有得到任何响应,但如果我没有提及任何dataType
,我将获得页面的 HTML 格式。
public String list(){
JSONObject jo = new JSONObject();
try {
Iterator it = findList.iterator();
while(it.hasNext()){
SearchResult part = (SearchResult) it.next();
jo.put("col1",part.getcol1());
jo.put("col2",part.getcol2());
}
log.debug("--------->:"+jo.toString());
} catch (Exception e) {
log.error(e);
}
return jo.toString();
}
struts.xml:
<package name="default" namespace="/ajax" extends="json-default">
<action name="finder"
class="action.Part" method="finder" name="finder">
<result type="json" />
</action>
</package>
JSP 页面:
<div id="dialog-form" >
<form action="" id="channelfinder">
<textarea id="products" name="prodnbr"<s:property value='prodNbr'/>
</form>
</div>
控制台错误:
org.apache.struts2.dispatcher.Dispatcher - 找不到动作或结果没有为动作动作定义结果。部分和结果{“col1”:“col1”,“col2”:“col2”}
web.xml
:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>/parts</display-name>
<description>Parts List Web App</description>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorPage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/errorPage.jsp</location>
</error-page>
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
我没有获取 jQuery 成功的数据。请纠正我,这里有什么问题?