简体   繁体   English

使用jquery.Ajax()取消请求

[英]Cancel a request using jquery.Ajax()

I am currently setting up a projects page with a series of links to project images, these images are loaded in using jquery load at the moment. 我目前正在建立一个项目页面,其中包含一系列指向项目图像的链接,这些图像目前正在使用jquery加载进行加载。 However if I click a link then click a different link in quick succession it seems to load the images from the first link rather than cancelling that request and loading the second link 但是,如果我单击一个链接,然后快速单击另一个链接,那似乎是从第一个链接加载图像,而不是取消该请求并加载第二个链接

I gather from what I have been reading that the best method use would be to swap jquery load for jquery Ajax, the only issue is that I don't quite understand how to achieve this with jquery ajax, I would be grateful if someone could give me a starting point 从我一直在阅读的内容中得知,最好的方法是将jquery的负载交换为jquery Ajax,唯一的问题是我不太了解如何使用jquery ajax来实现这一点,如果有人愿意,我将不胜感激我一个起点

Thanks 谢谢

You will probably want to read: Abort Ajax requests using jQuery They explain that very thing. 您可能需要阅读: 使用jQuery中止Ajax请求他们解释了这一点。 For some reason, StackOverflow wants to convert this to a comment, not an answer, so I've added this sentence. 由于某些原因,StackOverflow希望将其转换为注释,而不是答案,因此我添加了这句话。

I'm assuming right now, you're calling something like this: 我现在假设,您正在调用这样的内容:

$('#mydiv').load('projects.php?page=1');

The exact equivalent using ajax() would be: 使用ajax()的精确等效项为:

$.ajax('projects.php?page=1', {
  success: function(html) {
    $('#mydiv').html(html);
  }
});

I'm guessing people told you that you would want to abort the old request and start a new one? 我猜人们告诉过您,您想中止旧请求并开始新请求吗? You can do that by saving the jqXHR object returned by ajax() and using it later: 您可以通过保存ajax()返回的jqXHR对象并在以后使用它来做到这一点:

var jqXHR = $.ajax('first url', ...);

// Later
jqXHR.abort();
var jqXHR = $.ajax('second url', ...);

Hopefully that's enough to get you started, let me know if you have questions. 希望这足以让您入门,如果您有任何疑问,请告诉我。

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

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