【问题标题】:Multiple Value of a javascript argumentjavascript参数的多个值
【发布时间】:2018-07-02 00:22:44
【问题描述】:

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

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

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

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

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

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

请帮忙

【问题讨论】:

  • 您如何发送和接收这些消息?
  • @gurvinder372 var data = JSON.stringify({ "from": "InfoSMS", "to": [ "41793026727", "41793026834" ], "text": "测试短信。" } ); var xhr = new XMLHttpRequest(); xhr.withCredentials = 假; xhr.addEventListener("readystatechange", function () { if (this.readyState === this.DONE) { console.log(this.responseText); } });
  • @gurvinder372 第二部分 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);

标签: javascript android json arguments infobip-api


【解决方案1】:

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

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的其余逻辑

【讨论】:

  • @FiifiDes 你忘了删除stringifydata
  • @FiifiDes 只是不要使用它,或者如果您的输入已经被字符串化,然后执行JSON.parse(str) 以取回对象。
  • @FiifiDes 不!正如我之前提到的,要么不要像之前那样进行字符串化,要么使用 JSON.parse 从字符串中取回对象。
  • @FiifiDes 抱歉,我不明白您的上一条评论与该问题有何关联。
  • 你可以通过obj.contact得到它
猜你喜欢
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多