Find Jobs
Hire Freelancers

Ajax call inside a loop not working

$10-30 USD

Completed
Posted almost 10 years ago

$10-30 USD

Paid on delivery
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 [login to view URL] = [login to view URL]([]); //Populate the noticias list with the request result =>>> THAT's WHERE THE PROBLEM IS! (function(tpc){ $.get(apiURL + "/topicos/" + [login to view URL] + "/noticias/", function(data) { if (data!=null && [login to view URL] > 0) { var auxNoticias = new Array(); $(data).each(function(){ [login to view URL](this); }); [login to view URL](auxNoticias); } }) })(topico); //montagem do VM das fases [login to view URL] = false; [login to view URL] = false; [login to view URL] = false; [login to view URL] = false; $([login to view URL]).each(function(idx, fase) { if ([login to view URL] == 1) { [login to view URL] = true; }else if ([login to view URL] == 2) { [login to view URL] = true; }else if ([login to view URL] == 3) { [login to view URL] = true; }else if ([login to view URL] == 4) { [login to view URL] = true; } }); //montagem do VM das perguntas $([login to view URL]).each(function(idx, pergunta) { [login to view URL] = [login to view URL](false); [login to view URL] = [login to view URL](false); [login to view URL] = [login to view URL](''); [login to view URL] = [login to view URL](false); [login to view URL] = [login to view URL](function() { return "Olá, eu gostaria de saber mais informações sobre \"" + pergunta.title+"\""; }); [login to view URL] = function() { var _pergunta = this; [login to view URL](true); } [login to view URL] = function() { var _pergunta = this; [login to view URL](function(){ [login to view URL](false); [login to view URL](![login to view URL]()); }); } [login to view URL] = function (cb) { var _pergunta = this; if ([login to view URL]() == '') { [login to view URL](true); $.get(apiURL + "/perguntas/" + [login to view URL], function(data) { [login to view URL]([login to view URL]); cb(); }); }else{ cb(); } } }); }); [login to view URL] = [login to view URL](topicosVM);;
Project ID: 5997085

About the project

4 proposals
Remote project
Active 10 yrs ago

Looking to make some money?

Benefits of bidding on Freelancer

Set your budget and timeframe
Get paid for your work
Outline your proposal
It's free to sign up and bid on jobs
Awarded to:
User Avatar
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 in 1 day
3.6 (1 review)
0.8
0.8
4 freelancers are bidding on average $31 USD for this job
User Avatar
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 correcting the problem, I'll explain the cause so you don't run into the same issue again in the future. I should be able to have you the corrected script within an hour or two of receiving the script to work on. Regards, John
$30 USD in 1 day
5.0 (2 reviews)
4.3
4.3
User Avatar
A proposal has not yet been provided
$29 USD in 1 day
5.0 (10 reviews)
3.2
3.2
User Avatar
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 in 1 day
5.0 (4 reviews)
2.5
2.5
User Avatar
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 in 1 day
5.0 (4 reviews)
1.7
1.7

About the client

Flag of BRAZIL
Brasília, Brazil
4.8
10
Payment method verified
Member since May 16, 2014

Client Verification

Thanks! We’ve emailed you a link to claim your free credit.
Something went wrong while sending your email. Please try again.
Registered Users Total Jobs Posted
Freelancer ® is a registered Trademark of Freelancer Technology Pty Limited (ACN 142 189 759) & Freelancer Online India Private Limited (CIN U93000HR2011FTC043854)
Copyright © 2024 Freelancer Technology Pty Limited (ACN 142 189 759)
Loading preview
Permission granted for Geolocation.
Your login session has expired and you have been logged out. Please log in again.