I gang

Ajax call inside a loop not working

I have a JS code that has a list of elements (name "topicos"). This topicos list has a property named "noticias" wich may have to be published in parallel and afeter the page is loaded. The thing is that when trying to use jquery each() to iterate over the "topicos" list and using the jquery ajax() command to get the respective "noticia" for the current topico, the response is assigned to the wrong "topico". It appears to be a object reference issue; I mean, the variable topico has a reference to one object when the $.ajax is called but when the success callback is invoked the variable topic is referencing another object.

The code:

//topicosVM - list with topics

$(topicosVM).each(function(idx, topico) {

//recuperação das noticias vinculadas

[url removed, login to view] = [url removed, login to view]([]);

//Populate the noticias list with the request result =>>> THAT's WHERE THE PROBLEM IS!

(function(tpc){

$.get(apiURL + "/topicos/" + [url removed, login to view] + "/noticias/", function(data) {

if (data!=null && [url removed, login to view] > 0) {

var auxNoticias = new Array();

$(data).each(function(){

[url removed, login to view](this);

});

[url removed, login to view](auxNoticias);

}

})

})(topico);

//montagem do VM das fases

[url removed, login to view] = false;

[url removed, login to view] = false;

[url removed, login to view] = false;

[url removed, login to view] = false;

$([url removed, login to view]).each(function(idx, fase) {

if ([url removed, login to view] == 1) {

[url removed, login to view] = true;

}else if ([url removed, login to view] == 2) {

[url removed, login to view] = true;

}else if ([url removed, login to view] == 3) {

[url removed, login to view] = true;

}else if ([url removed, login to view] == 4) {

[url removed, login to view] = true;

}

});

//montagem do VM das perguntas

$([url removed, login to view]).each(function(idx, pergunta) {

[url removed, login to view] = [url removed, login to view](false);

[url removed, login to view] = [url removed, login to view](false);

[url removed, login to view] = [url removed, login to view]('');

[url removed, login to view] = [url removed, login to view](false);

[url removed, login to view] = [url removed, login to view](function() {

return "Olá, eu gostaria de saber mais informações sobre \"" + pergunta.title+"\"";

});

[url removed, login to view] = function() {

var _pergunta = this;

[url removed, login to view](true);

}

[url removed, login to view] = function() {

var _pergunta = this;

[url removed, login to view](function(){

[url removed, login to view](false);

[url removed, login to view](![url removed, login to view]());

});

}

[url removed, login to view] = function (cb) {

var _pergunta = this;

if ([url removed, login to view]() == '') {

[url removed, login to view](true);

$.get(apiURL + "/perguntas/" + [url removed, login to view], function(data) {

[url removed, login to view]([url removed, login to view]);

cb();

});

}else{

cb();

}

}

});

});

[url removed, login to view] = [url removed, login to view](topicosVM);;

Færdigheder: AJAX, Javascript, jQuery / Prototype

Se mere: vm where, prototype request, prototype reference, idx 2, idx 1, get prototype, ajax.js, call id, javascript function response, javascript callback, jquery callback, call function, jquery elements, javascript null, ajax call, tpc, callback jquery, jquery populate, ajax return, javascript toggle, saber, load page page jquery, jquery page load function, javascript object array, jquery variable length

Om arbejdsgiveren:
( 10 bedømmelser ) Brasília, Brazil

Projekt-ID: #5997085

Tildelt til:

vsrkrishnan

Hi, I have more than 4 years of experience in developing rich internet applications and worked a lot with HTML, JS/JQuery and CSS. I think I can definitely help you with this issue. Thanks, Shiva

$25 USD på 1 dag
(1 bedømmelse)
0.8

4 freelancere byder i gennemsnit $31 for dette job

ActazenCorp

Hi, I've reviewed your project description and the code you provided and I've located the problem, its a somewhat common error so don't feel bad - but it's easy to correct so you can move forward. In addition to co Mere

$30 USD på 1 dag
(2 bedømmelser)
4.3
guangzhen122

A proposal has not yet been provided

$29 USD på 1 dag
(10 bedømmelser)
3.2
janakapdj

Hi I have 5 year experience and can look your job now I think I could fixed this If you wish I would like to discuss with you now :)

$35 USD på 1 dag
(4 bedømmelser)
2.5
kotish

Hello Sir, I was worked lot of projects using AJAX. I can complete this task ASAP. Looking forward to our positive reply. Thanks

$35 USD på 1 dag
(4 bedømmelser)
1.7