Question

I'm using sailsjs to insert data from GET parameter of the URL (mydomain.com/prod_master/addsupp).

The page is /prod_master/addsupp which is accepting GET parameters to insert in database.

In javascript I need to do loop and insert more than one record following is the javascript code i'm using:

<script>
for(var i=2;i<(rCount);i++)
{
supplier=tbl.rows[i].cells[3].children[0].value;
del_lead_time=tbl.rows[i].cells[4].children[0].value;
min_qty=tbl.rows[i].cells[5].children[0].value;
window.location="/prod_master/addsupp?supplier="+supplier+"&del_lead_time="+del_lead_time+"&min_qua="+min_qty;
}
</script>

However I can confirm that using my url mydomain.com/prod_master/addsupp?supplier=val&del_lead_time=val2&min_qua=val3 its adding records to database perfectly but in loop if i use window.location=url then its not working.

Any solution?

Note: if is there any jQuery solution then also let me know.

Was it helpful?

Solution

In loop you can't use window.location=url to call any url and do some task. Because javascript execution will be faster than you think. Once it has replaced URL in window.location, in next loop it will replace same and it will conflict with previous call.

Better approach would be calling that URL using ajax call.

I'm giving you psudo code using jQuery to do GET request.

<script>
for(var i=2;i<(rCount);i++)
{
    supplier=tbl.rows[i].cells[3].children[0].value;
    del_lead_time=tbl.rows[i].cells[4].children[0].value;
    min_qty=tbl.rows[i].cells[5].children[0].value;

    urlToCall="/prod_master/addsupp?supplier="+supplier+"&del_lead_time="+del_lead_time+"&min_qua="+min_qty;

    $.get(urlToCall, function(response){
      console.log(response); //you might want to see returned response
    });
}
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top