Próbuję zadzwonić do danych za pomocą AJAX, ale otrzymuję tylko dane po drugim kliknięciu. Mam dodanie konsoli.log, aby to zrozumieć.
$(document).ready(function(){
var target = "http://neoldes/cs/Satellite?pagename=SiteEntry_LT_EM/ConsumptionStatistics/ConsumptionPerHour";
var component;
$("header.mod_info_top").on("click", function() {
$(this).siblings(".cs_consumption_hour").empty();
loadConsPerHour( target ).success(function (data) {
component = data;
});
console.log("component");
console.log(component);
$(this).next().append(component);
});
});
function loadConsPerHour( dir ){
return $.ajax({
url: dir,
data: { "accountNumber": $("#count_id").val(),"addressService": $("#addressService").val(), "department": $("#department").val(), "municipality": $("#municipality").val(), "borderTrade": $("#borderTrade").val() }
});
}
To jest powrót w pierwszym kliknięciem konsoli:
component
undefined
Drugi kliknięcie:
component
<div id="component"> ... { more html } ...</div>
Z góry dziękuję i daj mi znać, jeśli mogę poprawić mój kod.
0
AndreFontaine
13 sierpień 2014, 20:13
2 odpowiedzi
Najlepsza odpowiedź
Jego dlatego, że funkcja konsoli.log () jest wykonywana przed zakończeniem AJAX. Będziesz musiał wykonywać Call Console.log () po zakończeniu AJAX, w ten sposób:
$(document).ready(function(){
var target = "http://neoldes/cs/Satellite?pagename=SiteEntry_LT_EM/ConsumptionStatistics/ConsumptionPerHour";
var component;
$("header.mod_info_top").on("click", function() {
var targetElement = $(this);
$(this).siblings(".cs_consumption_hour").empty();
loadConsPerHour( target ).success(function (data) {
component = data;
console.log("component");
console.log(component);
targetElement.next().append(component);
});
});
});
function loadConsPerHour( dir ){
return $.ajax({
url: dir,
data: { "accountNumber": $("#count_id").val(),"addressService": $("#addressService").val(), "department": $("#department").val(), "municipality": $("#municipality").val(), "borderTrade": $("#borderTrade").val() }
});
}
Zaloguje się:
component
<div id="component"> ... { more html } ...</div>
0
Azeem
13 sierpień 2014, 16:54
Spróbuj przenieść kod dołączenia do wewnątrz połączenia odcinającego.
$("header.mod_info_top").on("click", function() {
$(this).siblings(".cs_consumption_hour").empty();
loadConsPerHour( target ).success(function (data) {
component = data;
console.log("component");
console.log(component);
$(this).next().append(component);
});
});
0
Callebe
13 sierpień 2014, 16:16