简体   繁体   English

如何使用Jquery AJAX调用Spring控制器方法

[英]How do I call a Spring controller method with Jquery AJAX

I have the following Spring Controller 我有以下Spring Controller

@Controller
@RequestMapping("/accreq")

with the following mapping 使用以下映射

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

I'm trying to call this method with the following jquery ajax 我正在尝试使用以下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 being called even though the ajax method is being called when I click a button. 请帮我弄清楚为什么即使在单击按钮时调用ajax方法也没有调用Spring方法。 I have stepped through the script with firebug and it definitely hits the ajax function. 我已经使用firebug逐步完成了脚本,它肯定会触及ajax函数。

First, try to see what happens if you hit the URL manually in the browser 首先,尝试查看在浏览器中手动点击URL时会发生什么

If that is successful, turn on request tracking in firebug and see if firefox actually hits the URL (and what the response says) 如果成功,请打开firebug中的请求跟踪,看看firefox是否实际点击了URL(以及响应的内容)

try to add this to your jquery 尝试将此添加到您的jquery

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

and you will see if an error occurs 你会看到是否发生错误

which pattern are you using in your web.xml? 你在web.xml中使用哪种模式? Search for the tag and see which pattern are you using. 搜索标签并查看您使用的模式。

For example, if you are using: 例如,如果您使用:

<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");
    }
});

Is it possible that there is a server side exception occurring and that is why you are not seeing a response. 是否可能发生服务器端异常,这就是您没有看到响应的原因。 If you have debugging on make sure your exceptions are thrown and/or you have a log of the same. 如果您有调试,请确保抛出异常和/或您有相同的日志。

Also your comment is kinda ambiguous what do u mean the spring methid is not being called even though the ajax methid is being called. 你的评论也有点含糊不清是什么意思,即使ajax methid被调用,spring methid也没有被调用。 Can you please clarify. 你能澄清一下吗? Also waht would help is the firebug o/p of the call with a screenshot or seomthing.. also if firebug throws an error what error it is. 另外,如果firebug发出错误是什么错误,那么使用屏幕截图或seomthing也可以帮助调用firebug o / p。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM