[英]JQuery request aborted - method called twice in Spring MVC controller
I've been stuck on this problem for 3 days now, and can't find any helpful information online. 我已经在这个问题上停留了三天,无法在线找到任何有用的信息。
Here is my problem : I have a .jsp page displaying a list (in my controller, this is handled by @RequestMapping(value = "list"). When clicking on one of these items, I'm trying to display a div to show the details of the item. I use JQuery $ajax to send a request to the server to read the database (in my controller, this is handled by @RequestMapping(value = "getCompany")). 这是我的问题:我有一个显示列表的.jsp页面(在我的控制器中,这由@RequestMapping(value =“ list”处理)。单击这些项目之一时,我试图显示一个div以显示项目的详细信息,我使用JQuery $ ajax向服务器发送请求以读取数据库(在我的控制器中,这由@RequestMapping(value =“ getCompany”)处理)。
The issue I have is the request sent by JQuery aborts (type "NS_BINDING_ABORTED" in HttpFox). 我遇到的问题是JQuery中止发送的请求(在HttpFox中键入“ NS_BINDING_ABORTED”)。 It's working fine when I enter the URL manually in my browser. 当我在浏览器中手动输入URL时,它工作正常。
I noticed something that seems strange to me : when the request is sent to the controller, it executes the method (getCompany), return the result (using @ResponseBody), but right after the "list" method is invoked despite I have no redirection instruction. 我注意到对我来说似乎有些奇怪:将请求发送到控制器时,它执行方法(getCompany),返回结果(使用@ResponseBody),但是尽管没有重定向,但是在调用“ list”方法之后指令。 Could the unwanted redirection be the cause of the aborted JQuery request ? 不必要的重定向是否会导致JQuery请求中止? Does somebody know how to avoid this redirection ? 有人知道如何避免这种重定向吗?
In my understanding, the getCompany method should just add its result to the response. 以我的理解,getCompany方法应仅将其结果添加到响应中。
Thank you for your help ! 谢谢您的帮助 !
Here is my script : 这是我的脚本:
$(document).ready(function(){
//F1
$(".company").click(function(){
var id = this.id;
$.ajax({
type: "GET",
url : 'getCompany',
data: {companyId:id},
success : function(jqXHR, textStatus)
{
alert("success");
var json = '${jsonCompany}';
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.status, textStatus, errorThrown);
},
complete : function(result, status)
{
var json = '${jsonCompany}';
}
});
}) //End F1
and my controller : 和我的控制器:
@Controller("company")
public class CompanyController extends CommonController{
CompanyService companyService;
/** to company.jsp */
@RequestMapping(value = "list")
public ModelAndView manageCompanies()
{
mav.clear();
mav.addObject("companiesList", getCompaniesList());
mav.addObject("companyForm",new CompanyForm());
mav.setViewName("admin/company");
return mav;
}
/** @return company */
@RequestMapping(value = "getCompany")
public @ResponseBody String getCompany(@RequestParam(value="companyId", required = true) String companyId)
{
Company company = companyService.getCompany(new Integer(companyId));
return companyService.toJSON(company).toString();
}
I finally found the reason of my problem, if it can help some others. 我终于找到了问题的原因,如果它可以帮助其他人。 The item of the list (a basic <td>
) used to invoke the $ajax GET had a link on it ( <a href"">
) and was submitting the url displayed in the browser => not a "wierd" redirection problem. 用于调用$ ajax GET的列表项(基本的<td>
)上具有链接( <a href"">
),并且正在提交浏览器中显示的URL =>而不是“奇怪的”重定向问题。 And answer to one of my question : yes, sending two requests at the same time (or almost) will kill one of the request (in my case, the $ajax one). 并回答我的问题之一:是的,同时(或几乎)发送两个请求将杀死其中一个请求(在我的情况下为$ ajax)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.