2014-10-29 67 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

相關問題