i am also facing the same issue. I have created the code as shown in the link above, but its returning whole HTML content...
Basically i have 2 view Resolvers now,
<bean id="ajaxViewResolver"
class="com.infocast.iweb.mvc.view.ajax.AjaxViewResolver" >
<property name="ajaxView">
<bean class="com.infocast.iweb.mvc.view.ajax.AjaxView" />
</property>
<property name="ajaxPrefix" value="ajax_"></property>
<property name="order" value="1"></property>
</bean>
<!-- Default View Resolver for Spring MVC Portlet -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
Now what happens is, when i call ajax using jquery i am getting whole http stuff in callback function.
var url = "<portlet:renderURL><portlet:param name='action' value='totalCountJson' /></portlet:renderURL>";
//var url = "stockscreener/total_match_count";
url +="&"+jQuery('input:hidden').serialize();
alert(url);
jQuery.ajax({ url: url,
data: {},
success: function(message) {
alert(message);
jQuery('input:hidden[@name="total_stock_items"]').val(message.totalMatches);
jQuery('div.secondary-nav').children('div.viewMatches').
html('<a href="#" class="black_button rfloat" onclick="javascript:loadDetailsPage();">VIEW '+message.totalMatches+' MATCHES</a>');
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status + " - " + xhr.statusText);
}
}
);
I want to get it using JSON object, as per the AjaxView
class form the link. So it should return the JSON object in the JS callback.
Is it regarding the view resolvers OR the proper content type not set? I use text/javascript
as a contentType,
public class AjaxView extends AbstractView {
private static final Log log = LogFactory.getLog(AjaxView.class);
/* (non-Javadoc)
* @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response)
throws Exception {
log.info("Resolving ajax request view - "+map);
JSONObject jsonObj = new JSONObject(map);
response.reset();
response.setContentType( "text/javascript; charset=UTF-8" );
response.getWriter().write(jsonObj.toString());
}
}
Please help..