简体   繁体   中英

Uncaught ReferenceError: error is not defined in Ajax Callback

In my App, I get a list of Events from a Sharepoint Calendar List. That part works perfectly. However after I get the collection of results, for each item I need to get the Display Form Url, which is another REST Call with the ListItem ID.

However I get the error below, but I still dont know what the problem might be

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

I based my code on this answer: https://sharepoint.stackexchange.com/questions/119236/how-to-get-the-display-form-url-using-rest

My adapted code is like this:

   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");
                }
            });
    }
}

Under //Usage :

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

error does not seem to be defined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM