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

时间:2017-10-25 08:04:47

标签: jquery rest sharepoint sharepoint-online

尝试将数据发布到共享点列表时,我有一个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));

                    });
            });

1 个答案:

答案 0 :(得分:0)

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

Options +MultiViews
RewriteEngine On 
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]