繁体   English   中英

未捕获的ReferenceError:Ajax回调中未定义错误

[英]Uncaught ReferenceError: error is not defined in Ajax Callback

在我的应用程序中,我从Sharepoint日历列表中获取事件列表。 该部分工作完美。 但是,在获得结果集合之后,对于每个项目,我都需要获取显示表单网址,这是另一个具有ListItem ID的REST调用。

但是我得到下面的错误,但我仍然不知道可能是什么问题

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

//Usage

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

error似乎未定义

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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