2015-09-15 80 views
2

我想中止Ajax请求是在其他功能 举例:中止Ajax请求的其他功能

var xhr = false; 
$(function(e) { 
    $(document).on('click', '.menue-button', function(e) { 
     // Some JS 
     xhr = $.ajax({ 
      type: "POST", 
      url: index.php, 
      //The ajax functions 
      }); 
     }); 
}); 

$(document).on('click', '#progress-abort', function() {   
    xhr.abort(); 
}); 

调试器说:

Uncaught TypeError: Cannot read property 'abort' of undefined

我知道为什么,但我不知道如何解决这个问题

+0

使用trigers .... –

回答

0

它尝试了以下内容:

var xhr = false; 
$(function(e) { 
$(document).on('click', '.menue-button', function(e) { 
    // Some JS 
    xhr = $.ajax({ 
     type: "POST", 
     url: index.php, 
     //The ajax functions 
     }); 
    $(document).on('click', '#progress-abort1', function() { 
    xhr.abort(); 
    }); 

    $(document).on('click', '#progress-abort', function() {   
     $('#progress-abort1').trigger('click'); 
    }); 
}); 

'#progress-abort1不触发。怎么了?