0

我正在使用 Javascript 函数将一条消息发送到多个目的地或号码:

var data = JSON.stringify({
  "from": "InfoSMS",
  "to": [
    "41793026727",
    "41793026834"
  ],
  "text": "Test SMS."
});

现在在我的应用程序中,我将“to”作为参数 - arg.receivers,其中的数字将作为 41793026727,41793026834 提供给代码

但是,参数-arg.receivers 被识别为单个数字参数,因此它不会发送 SMS

因此,如果 arg.receivers 只是一个数字,例如。41793026834 它发送短信,但如果它像上面那样是多个它不发送。

有没有办法将参数设置为多个数字?

请帮忙

4

1 回答 1

0

您可以迭代数据(不要字符串化)并一一发送消息。

var data = {
  "from": "InfoSMS",
  "to": ["41793026727", "41793026834"],
  "text": "Test SMS."
};

data.to.forEach( function(to){
   sendMessage( JSON.stringify( Object.assign( {}, {to:to}, data )  ) ); //creating one message each for **to**
});

function sendMessage(data) {
  var xhr = new XMLHttpRequest();
  xhr.withCredentials = false;
  xhr.addEventListener("readystatechange", function() {
    if (this.readyState === this.DONE) {
      console.log(this.responseText);
    }
  });
  xhr.open("POST", "api.infobip.com/sms/1/text/single");
  xhr.setRequestHeader("authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
  xhr.setRequestHeader("content-type", "application/json");
  xhr.setRequestHeader("accept", "application/json");
  xhr.send(data);
}

编辑

如果你已经得到一个字符串,那么做

data = JSON.parse( data );

然后继续执行其余的逻辑forEach

于 2018-01-23T08:50:58.457 回答