2014-10-29 79 views
0

在我的应用程序中,我从Sharepoint日历列表中获取事件列表。那部分工作完美。 但是,当我得到结果的集合后,我需要获取Display Form Url,这是带有ListItem ID的另一个REST Call的每个项目。Uncaught ReferenceError:未在Ajax回调中定义错误

但是我得到下面的错误,但我仍然不知道

Uncaught ReferenceError: error is not defined App.js:87(anonymous function) App.js:87$.ajax.error App.js:40c jquery-1.9.1.min.js:22p.fireWith jquery-1.9.1.min.js:22k jquery-1.9.1.min.js:24send.r 

我根据我对这个答案代码的问题可能是什么: https://sharepoint.stackexchange.com/questions/119236/how-to-get-the-display-form-url-using-rest

我适应代码是这样的:

var SPHostUrl; 
var SPAppWebUrl; 
var ready = false; 

// this function is executed when the page has finished loading. It performs two tasks: 
// 1. It extracts the parameters from the url 
// 2. It loads the request executor script from the host web 
$(document).ready(function() { 
    var params = document.URL.split("?")[1].split("&"); 
    for (var i = 0; i < params.length; i = i + 1) { 
     var param = params[i].split("="); 
     switch (param[0]) { 
      case "SPAppWebUrl": 
       SPAppWebUrl = decodeURIComponent(param[1]); 
       break; 
      case "SPHostUrl": 
       SPHostUrl = decodeURIComponent(param[1]); 
       break; 
     } 
    } 

    // load the executor script, once completed set the ready variable to true so that 
    // we can easily identify if the script has been loaded 
    $.getScript(SPHostUrl + "/_Layouts/15/SP.RequestExecutor.js", function (data) { 
     ready = true; 
     getItems(); 
    }); 
}); 

function getListItemFormUrl(webUrl, listName, listItemId, formTypeId, complete, failure) { 
    $.ajax({ 
     url: webUrl + "/_api/web/lists/GetByTitle('" + listName + "')/Forms?$select=ServerRelativeUrl&$filter=FormType eq " + formTypeId, 
     method: "GET", 
     headers: { "Accept": "application/json; odata=verbose" }, 
     success: function (data) { 
      var url = data.d.results[0].ServerRelativeUrl + '?ID=' + listItemId 
      complete(url); 
     }, 
     error: function (data) { 
      failure(data); 
     } 
    }); 
} 



// this function retrieves the items within a list which is contained within the parent web 
function getItems() { 

    // only execute this function if the script has been loaded 
    if (ready) { 

     // the name of the list to interact with 
     var listName = "Events"; 

     // the url to use for the REST call. 
     var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" + 

      // this is the location of the item in the parent web. This is the line 
      // you would need to change to add filters, query the site etc 
      // "/web/lists/getbytitle('" + listName + "')/items?" + 
      "/web/lists/getbytitle('" + listName + "')/items?$select=Title,Category,EventDate,Description,EncodedAbsUrl,ID" + 
      "&@target='" + SPHostUrl + "'"; 

     // create new executor passing it the url created previously 
     var executor = new SP.RequestExecutor(SPAppWebUrl); 

     // execute the request, this is similar although not the same as a standard AJAX request 
     executor.executeAsync(
      { 
       url: url, 
       method: "GET", 
       headers: { "Accept": "application/json; odata=verbose" }, 
       success: function (data) { 

        // parse the results into an object that you can use within javascript 
        var results = JSON.parse(data.body); 
        var events = []; 
        $.each(results.d.results, function (i, obj) { 

         //Usage 
         getListItemFormUrl(SPAppWebUrl, 'Calendar', obj.ID, 4, 
          function (url) { 
           console.log('Display from url for list item: ' + url); 
          }, 
          function (sender, args) { 
           console.log(JSON.stringify(error)); 
          }) 

         //use obj.id and obj.name here, for example: 
         var event = { 
          date: Date.parse(obj.EventDate).toString(), 
          type: obj.Category, 
          title: obj.Title, 
          description: obj.Description, 
          url: obj.EncodedAbsUrl + 'DispForm.aspx?ID=' + obj.ID 
         } 
         events.push(event); 
        }); 
        var myJsonString = JSON.stringify(events); 

        $("#eventCalendarInline").eventCalendar({ 
         jsonData: events, 
         openEventInNewWindow: true, 
         showDescription: true, 
         txt_GoToEventUrl: "Go to event" 
        }); 

        Communica.Part.init(); 

       }, 
       error: function (data) { 

        // an error occured, the details can be found in the data object. 
        alert("Ooops an error occured"); 
       } 
      }); 
    } 
} 

回答

1

//Usage

function (sender, args) { 
    console.log(JSON.stringify(error)); 
}) 

error似乎没有定义

+0

不是js专家,该如何解决?只是取代发件人,错误的参数? – 2014-10-29 14:39:42

+0

您可以删除该行,无论如何它只是一个'console.log'。 – JimmyRare 2014-10-29 18:27:08

相关问题