-1

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.

Muhammed Raheez PC
  • 343
  • 1
  • 4
  • 18
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Jeremy Thille Feb 14 '18 at 09:07

3 Answers3

1

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)
    });
}
john Smith
  • 15,471
  • 10
  • 66
  • 107
-1

This is why:

return xmlHttp.responseText;

You should also read up on synchronous and asynchronous requests.

Asgeirr
  • 82
  • 10
-3
function message_api() {
    var phone_nubmers = ['0000000','****','2222','4744'];
    var message="test message from ";

    phone_nubmers.forEach(function(entry) {
        var url='http://123.123.123.123:8080/sendsms/bulksms?username=*****-****&password=*********&type=0&dlr=1&destination='+entry+'&source=******&message='+message;

        $.ajax({
            url:url,  
            success: function(data) {
                return data; 
            }
        });
    });
}
Nick Parsons
  • 31,322
  • 6
  • 25
  • 44
  • This is a lump of mostly illegible completely unformatted code (look at the preview of your answer, make use of the toolbar for formatting, and read the help) that's entirely lacking anything resembling an explanation for what you've changed or why that should help. – Quentin Jan 26 '20 at 18:35
  • As @Quentin said can you please add an explanation of what you've done and why it solves the problem? Also remember to always format your code. – Marcelo The Mage Coder Jan 26 '20 at 18:39