How can I name a Spring controller method using jQuery AJAX

I have the following spring controller

@Controller
@RequestMapping("/accreq")

with the following display

@RequestMapping(value = "/defRoles", method=RequestMethod.GET)
public @ResponseBody String loadDefaultRoles(
    @RequestParam(value="idGroup", required=false) String groupID
    throws ServletException{

I am trying to call this method with the following jquery ajax

$.ajax({
type: 'GET',
url: '/accreq/defRoles',
data: {idGroup: $('#infoGroup').val() },
success: function() {
    alert("success");
    }
});

Please help me figure out why the Spring method is not called, even if the ajax method is called when the button is clicked. I went through a script with firebug and it definitely gets into the ajax function.

+3
source share
4 answers

First try to see what happens if you click the URL manually in the browser

If successful, enable request tracking in firebug and see if firefox really accesses the URL (and what the answer says)

+4
source

try adding this to your jquery

error: function(jqXHR, textStatus, errorThrown) {
    alert("error:" + textStatus + " exception:" + errorThrown);
    }
}

,

+1

web.xml? , .

, :

<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>

you should use something like this:

$.ajax({
type: 'GET',
url: '/accreq/defRoles.html',
data: {idGroup: $('#infoGroup').val() },
success: function() {
    alert("success");
    }
});
+1
source

Is it possible that an exception has occurred on the server side, and therefore you do not see the answer. If you have debugging, make sure your exceptions are thrown and / or you have a log of the same.

Also your comment is a bit ambiguous, which means that spring metid is not called even when ajax methid is called. Could you clarify. Also waht will help firebug o / p calling with a screenshot or seomthing .. also, if firebug gives an error, what an error.

0
source

Source: https://habr.com/ru/post/1790229/


All Articles