Home How to send request from JavaScript inside in for loop
Reply: 2

How to send request from JavaScript inside in for loop

raheez
1#
raheez Published in 2018-02-14 09:02:22Z

I want to send bulk sms through javascript request. I tried like the following code but its only sending to first phone number only. I want to send it to the array listed numbers

<script type="text/javascript">
function message_api() 
{
    var phone_nubmers=['0000000','****','2222','4744'];
    for (var i = 0; i < phone_nubmers.length; i++) 
    {
        api(phone_nubmers[i]);
    }
}
function api(numbers) 
{
    var message="test message from ";
    var url='http://123.123.123.123:8080/sendsms/bulksms?username=*****-****&password=*********&type=0&dlr=1&destination='+numbers+'&source=******&message='+message;
    $.ajax({
        url:url,  
        success: function(data) {
            return data; 
        }
    });
}

I tried like this.

john Smith
2#
john Smith Reply to 2018-02-14 09:42:48Z

the return statement inside the loop will definetly stop the execution, this is why it only works once

you need another function you pass the result so the loop continues, sth. like the following will work

function takeResp(resp){
  console.log(resp)
}

function api(numbers) 
{
    var message="test message from ";
    var url='http://123.123.123.123:8080/sendsms/bulksms?username=*****-****&password=*********&type=0&dlr=1&destination='+numbers+'&source=******&message='+message;
    $.ajax({
        url:url,  
        success: takeResp(data)
    });
}
Asgeirr
3#
Asgeirr Reply to 2018-02-14 09:09:41Z

This is why:

return xmlHttp.responseText;

You should also read up on synchronous and asynchronous requests.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.346122 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO