JavaScript处理警报,没有工作

时间:2014-01-14 00:10:46

标签: javascript sharepoint alert

我写了一个脚本,用于更新SharePoint列表中的字段,但是当我删除警报(“test”)时;它停止工作。这是我的代码:

<script type="text/javascript">

$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(loadConstants, "sp.js"); });        

function loadConstants() {

    var userid= _spPageContextInfo.userId;
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept" : "application/json;odata=verbose" };
    $.ajax({
      url : requestUri,
      contentType : "application/json;odata=verbose",
      headers : requestHeaders,
      success : onSuccess,
      error : onError
    });

    function onSuccess(data, request){
        var loginName = data.d.Title;

        var ctx = new SP.ClientContext("site name");
        var oList = ctx.get_web().get_lists().getByTitle('list name');
        this.oListItem = oList.getItemById(1);          

        ctx.load(this.oListItem);
        ctx.executeQueryAsync(Function.createDelegate(this, function () {                                                   
                                                    this.oListItem.set_item('Read', loginName + '  ' + getTodayDate(););
                                                    }),  function (sender, args) { alert('Error occured' + args.get_message());});    

        //HERE IS THE ALERT:
        //alert("test");
        this.oListItem.update();            

        ctx.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded), 
            Function.createDelegate(this, this.onQueryFailed)
        );
    }
    function onError(error) {
        alert("error");
    }
    function getTodayDate() {
        //code that gets today's date
        return today;
    }
}

</script>

提前感谢您对此问题的任何帮助!

1 个答案:

答案 0 :(得分:1)

我不知道这个API,但看起来你正在将两个回调传递给executeQueryAsync()

第一个实际上首先传递给Function.createDelegate()。我假设返回一个新函数。无论如何,您传递给的匿名函数似乎用于响应查询。因此,任何依赖于返回数据的代码都应放在那里 $(document).ready(function(){ExecuteOrDelayUntilScriptLoaded(loadConstants,“sp.js”);});

function loadConstants() {

    var userid= _spPageContextInfo.userId;
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept" : "application/json;odata=verbose" };
    $.ajax({
      url : requestUri,
      contentType : "application/json;odata=verbose",
      headers : requestHeaders,
      success : onSuccess,
      error : onError
    });

    function onSuccess(data, request){
        var loginName = data.d.Title;

        var ctx = new SP.ClientContext("site name");
        var oList = ctx.get_web().get_lists().getByTitle('list name');
        this.oListItem = oList.getItemById(1);          

        ctx.load(this.oListItem);
        ctx.executeQueryAsync(Function.createDelegate(this, function () {                                                   
                                  this.oListItem.set_item('Read', loginName + '  ' + getTodayDate();

// run after the executeQueryAsync response arrives---vvvvvv
                                  this.oListItem.update();            

                                  ctx.executeQueryAsync(
                                     Function.createDelegate(this, this.onQuerySucceeded), 
                                     Function.createDelegate(this, this.onQueryFailed)
                                  );
                              }),  
                              function (sender, args) { 
                                  alert('Error occured' + args.get_message());
                              });    
    }
    function onError(error) {
        alert("error");
    }
    function getTodayDate() {
        //code that gets today's date
        return today;
    }
}

现在,在响应返回的查询而触发该回调之前,不会调用您的.update()代码。