在数组

时间:2018-05-04 16:46:57

标签: javascript jquery ajax

我的功能如下:

function sendEmail() {

var appweburl = _spPageContextInfo.webAbsoluteUrl;
var urlTemplate = appweburl + "/_api/SP.Utilities.Utility.SendEmail";


var fieldName = "Notificar a";
var body = "Descripción";
var _body = $("textarea[title^='" + body + "']");

var fullURL = window.location.href
var url = new URL(fullURL ); 
var ID = url.searchParams.get("ID");


var _PeoplePicker = $("div[title='" + fieldName + "']");
var emailSpan = $(_PeoplePicker).find('.sp-peoplepicker-userSpan');
var _sid ="";
var arrEmailID = [];

if (emailSpan.length > 0) {


$(emailSpan).each((index,el) => {
 arrEmailID.push(`'${$(el).attr('sid').split("|")[2]}'`);
});

} else {
       alert("null");
}

        var fieldUsuario = _sid;
        var fromMail = 'emaik@domain.com';
        var subject = 'Tarea Editada: ' + 'ID: ' +ID + ' ' + _body.val();
        var body = 'Se ha editado la tarea: '+ 'ID: ' + ID+ ' ' + 'Descripción: ' + _body.val() ;    

    $.ajax({
        contentType: 'application/json',
        url: urlTemplate,
        type: "POST",
        data: JSON.stringify({
            'properties': {
                '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
                'From': fromMail,
                'To': { 'results': [arrEmailID] },
                'Body': body, 
                'Subject': subject
            }
        }
      ),
        headers: {
            "Accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            var result = data.d.results;

        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
    });
}

正如你所看到的那样var arrEmailID = [];它填充了我收到的fore emailSpan,所以在一天的最后我有这样的东西:

var emailSpan = $('#_PeoplePicker').find('.sp-peoplepicker-userSpan'),
  arrEmailID = [];

$(emailSpan).each((index,el) => {
 arrEmailID.push(`'${$(el).attr('sid').split("|")[2]}'`);
});

console.log(arrEmailID);
.as-console-wrapper {max-height: 100% !important;top: 0;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="_PeoplePicker">
  <span sid="a|B|abc@abc.com" class="sp-peoplepicker-userSpan"></span>
  <span sid="a|B|test@abc.com" class="sp-peoplepicker-userSpan"></span>
  <span sid="a|B|ta@abc.com" class="sp-peoplepicker-userSpan"></span>
  <span sid="a|B|tessss@abc.com" class="sp-peoplepicker-userSpan"></span>
</div>

我想知道是否可以执行ajax调用foreach项arrEmailID并在To': { 'results': [arrEmailID] },中使用该项

1 个答案:

答案 0 :(得分:1)

我认为你正在寻找jQuery.when

伪代码可以按照以下方式执行:

jQuery.when(emails.map(email => {
    // your function details
    return jQuery.ajax({
       // your ajax configuration
    });
})
    .then((results) => {
       // handle the results here
    });