Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        6年前关闭。
                                                                                            
                
        
我写了一个脚本,该脚本更新了SharePoint列表中有效的字段,但是当我删除alert(“ 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>


预先感谢您对这个问题的任何帮助!

最佳答案

我不知道该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()代码将不会被调用,直到响应查询返回而触发该回调。

关于javascript - JavaScript具有警报功能,没有警报就无法工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21103868/

10-10 08:47