Reputation: 2682
I got stuck on something simple. When I make an ajax request, by some how it doesn't have enough time to make assignment to combonews
variable:
jQuery.ajax({
type: "POST",
url: "People.aspx/LoadComboNews",
data: "{\"id\":" + usrid + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
combonews = '';
setTimeout(function () { combonews = eval(msg.d); }, 500);
//combonews = eval(msg.d);
}
});
tried to add setTimeout as shown, but still when I want to alert combonews
it's empty.
When I alert msg.d
it's always have data ready.
Is there a way to extend the time for combonews = eval(msg.d);
to be perfomed?
UPDATE:
When I run it by binding to button click event, the assignment works fine
---------------------------------------
UPDATE2
function lcombo() {
jQuery('#combostart ~ option').remove();
//setTimeout((function () {
jQuery.ajax({
type: "POST",
url: "People.aspx/LoadComboNews",
data: "{\"id\":" + usrid + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (msg) {
// Replace the div's content with the page method's return.
combonews = '';
//setTimeout(function () { combonews = eval(msg.d); }, 500);
combonews = JSON.parse(msg.d);
}
});
//combonews = eval(combonews);
//alert(combonews);
jQuery(".chzn-select").chosen();
jQuery(".chzn-select-deselect").chosen({ allow_single_deselect: true });
var str = "";
if (combonews.length > 0)
for (var i in combonews) {
str += "<option value='" + combonews[i][0] + "'>" + combonews[i][1] + "</option>";
}
jQuery("#combooptions").append(str);
jQuery("#combooptions").val(draftid);
jQuery("#combooptions").trigger("liszt:updated");
}
and then I load lcombo() function. which works on click, but doesn't in some other consequences (I mean doesn't load the message to combonews)
THanks you
Upvotes: 1
Views: 1289
Reputation: 2682
@Adam that was the only solution I found, and I am making your comment as an answer
For simplicity sake, take all the code AFTER your ajax request, and drop it all inside your complete function. You were trying to do something with the variable "combonews" before your ajax request was complete. – Adam
But instead of complete
I used success
thanks for everyone
Upvotes: 1