在正文加载时我使用AJAX调用promise,如下所示:
var stateIDselected = '';
$(document).ready(function() {
var loadStates = $.ajax({
type: 'GET',
url: url + '/xxx/orn/getstates',
jsonpCallback: 'jsonCallback',
dataType: 'jsonp',
timeout: 7000,
jsonp: false,
cache: false,
success: function(response) {},
error: function(x, t, m) {}
}); // End of loading states
loadStates.done(function() {
$.ajax({
type: 'GET',
url: url + '/xxx/orn/getdistricts?stateID=' + stateIDselected,
jsonpCallback: 'jsonCallback',
dataType: 'jsonp',
timeout: 7000,
jsonp: false,
cache: false,
success: function(response) {},
error: function(x, t, m) {}
});
}); // End of loading districts
}); // ready call ends here
如果选择了一个州,我需要改革区选择标签:
$(document).on('change', '#statesList', function(event) {
var state_ID = $('#statesList').val().trim();
stateIDselected = state_ID;
// Here how can i use the ajax call present under loadStates.done()?
});
答案 0 :(得分:1)
您可以将xmlns:aop="http://www.springframework.org/schema/aop
处理程序中的逻辑电流提取到它自己的函数中,该函数可以从代码中的两个位置调用。试试这个:
done()
如果您不使用function getDistricts() {
$.ajax({
type: 'GET',
url: url + '/xxx/orn/getdistricts?stateID=' + stateIDselected,
jsonpCallback: 'jsonCallback',
dataType: 'jsonp',
timeout: 7000,
jsonp: false,
cache: false
});
}
loadStates.done(getDistricts); // End of loading districts
$(document).on('change', '#statesList', function(event) {
var state_ID = $('#statesList').val().trim();
stateIDselected = state_ID;
getDistricts();
});
和success
处理程序,也可以将其删除。