发布功能错误:找到没有类型名称的条目

问题描述 投票:0回答:1

尝试将数据发布到sharepoint列表时,我有一个ajax调用错误400。

例外是Microsoft.SharePoint.Client.InvalidClientQueryException并且消息是“找到了没有类型名称的条目,但未指定预期类型。要允许没有类型信息的条目,还必须在指定模型时指定预期类型。”

我的代码..

//Get form digest value
            function getFormDigest(webUrl) {
                return $.ajax({
                    url: webUrl + "/_api/contextinfo",
                    method: "POST",
                    headers: { "Accept": "application/json; odata=verbose" }
                });
            }



            //Create list item to add to sharepoint list
            function createListItem(webUrl, listName, itemProperties) {
                return getFormDigest(webUrl).then(function (data) {

                    return $.ajax({
                        url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
                        type: "POST",
                        processData: false,
                        data: JSON.stringify(itemProperties),
                        headers: {
                            "Accept": "application/json;odata=verbose",
                            "X-RequestDigest": data.d.GetContextWebInformation.FormDigestValue,
                            "Content-Type": "application/json;odata=verbose",
                            "X-HTTP-Method": "POST"
                        }
                    });
                });
            }

            //Getting the item type of a list
            function GetItemTypeForListName(name) {
                return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
            }


            //On button click
            $("#btn_post").click(function () {

                //Item properties
                var itemProperties = {
                    _metadata: { "type": GetItemTypeForListName("EmployeeBirthdayWishes") },
                    Title: $("#title").val(),
                    Wish: $("#birthday_wish").val(),
                    FullNames: $("#fullNames").val()
                };

                //Function call to create a list item
                createListItem(_spPageContextInfo.webAbsoluteUrl, "EmployeeBirthdayWishes", itemProperties)
                    .done(function (data) {
                        console.log(data);
                        console.log("Task has been created successfully");
                    }).fail(function (error) {
                        console.log(JSON.stringify(error));
                        alert(JSON.stringify(error));

                    });
            });
jquery rest sharepoint sharepoint-online
1个回答
0
投票

尝试使用以下方法更改itemProperties对象:

//Item properties
var itemProperties = {
    "__metadata": { "type": GetItemTypeForListName("EmployeeBirthdayWishes") },
    "Title": $("#title").val(),
    "Wish": $("#birthday_wish").val(),
    "FullNames": $("#fullNames").val()
};
© www.soinside.com 2019 - 2024. All rights reserved.