I have a MVCPortlet and I try to call ajax in a jsp page.
My jsp is as follows:
<%@include file="/html/init.jsp"%>
<portlet:resourceURL var="updateRatAjax" id="updateRatAjax" ></portlet:resourceURL>
<script type="text/javascript">
function selectRat(){
var rat = new Object();
rat.nom = $('#ratsList').val();
alert(rat.nom + ' '/* + portletURL*/);
$.ajax({
url: "${updateRatAjax}" ,
type: 'POST',
dataType: 'json',
data: JSON.stringify(article),
contentType: 'application/json',
mimeType: 'application/json',
success: function (data) {
alert('ajax' + article.nom);
}
});
alert('end of function');
}
</script>
....
<select class="offset2 span4 form-control" name=ratsList id = ratsList onchange="selectRat()">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<input name = "selectedRat" id ="selectedRat"></input>
My controler portlet resource is as folliws:
public class MainControllerPortlet extends MVCPortlet {
...
public void updateRatAjax( ResourceRequest request, ResourceResponse response)
throws IOException, PortletException {
System.out.println(" updateRatAjax");
}
}
I also tried to call the url this way into the js function:
url:"<%= updateRatAjax %>",
and
var portletURL = "<%= updateRatAjax %>";
url: portletURL,
no chance.
alert(rat.nom + ' '/* + portletURL*/);
oes display
but then nothing so the js method must be stopping when $.ajax({...}) is called.
The issue might have to do with the application not being able to map the resourceURL with the updateRatAjax method. In this project, the actionURL are correctly mapped to the controller's methods without I map them in web.xml nor I use any allocations. I took it that I don't need to map the portlet:resourceURL either, but I might be wrong there. First time I use portlet:resourceURL so I have no knowledge about them.
thx in advance for your assistance.
EDIT: following Sudakatux's answer, I changed the JS function to : ...
var portletURL = "${updateRatAjax}"; // "<%= updateRatAjax %>";//working //
alert(rat.nom + '\n ' + portletURL); //portletURL displays correctly with both options on previous line
$.ajax({
url: portletURL ,
type: 'POST',
dataType: 'text',
cache: false,
success: function (data) {
alert('success ajax' );
},
error: function(http, message, exc) {
alert('error ajax');
}
});
Strangely enough, I get the success message but the controller method is never called :
public void updateRatAjax( ResourceRequest request, ResourceResponse response)
throws IOException, PortletException {
//public void updateRatAjax(ActionRequest request, ActionResponse response) throws PortalException, SystemException {
System.out.println("MainController portlet : updateRatAjax");
}