DataTable Doesn't Repopulate After POST

DataTable Doesn't Repopulate After POST

zgoforthzgoforth Posts: 493Questions: 98Answers: 2

Hello, so I have created an awesome data table with an HTML form, hidden in the dropdown button. The goal of this is to Update the DataTable by sending the data to its corresponding subsite list, which should then automatically update the DataTable after it has been added. This past week has been brutal trying to figure out why it is doing what it is doing. Stack Overflow has been 0 help. SharePoint forums on MSDN has been 0 help. Its like every time I find what seems to be a solution, it will give me another error. The current error that I am getting, is "The parameter __metadata does not exist in method GetByTitle..

Here is a JSFiddle: https://jsfiddle.net/ek3tg67s/2/

Here is my code:

        function PostItem() {
            return getFormDigest("https://siteurl.sharepoint.com/sites/Projects/USMC/AMMO").then(function(digestData) {
                console.log(digestData.d.GetContextWebInformation.FormDigestValue);
                $.ajax({
                    async: true, // Async by default is set to “true” load the script asynchronously  
                    // URL to post data into sharepoint list  or your own url
                    url: "https://siteurl.sharepoint.com/sites/Projects/USMC/AMMO/_api/web/lists/getbytitle('AMMO Deliverables')",
                    method: "POST", //Specifies the operation to create the list item  
                    data: JSON.stringify({  
                        '__metadata': {  
                            'type': getItemTypeForListName("AMMO Deliverables") // it defines the ListEnitityTypeName  
                        }, 
                        Program: $("#dProgram").val(),
                        Deliverable: $("#dDeliverable").val(),
                        To: $("#dTo").val(),
                        Date: $("#dDate").val(),
                        Approved: $("#dApproved").val(),
                        Notes: $("#dNotes").val()
                    }),
                    headers: {
                        "content-type": "application/json;odata=verbose", //It defines the content type as JSON
                        "X-RequestDigest": digestData.d.GetContextWebInformation.FormDigestValue,
                        "Accept": "application/json;odata=verbose",
                        "If-Match": "*"
                    },
                    success: function(data) {
                        alert('Success'); // Used sweet alert for success message
                    },
                    error: function(error) {
                        alert(JSON.stringify(error));
                        console.log(JSON.stringify(error));
    
                    }
    
                });
            })
        }
            function getItemTypeForListName(listName) {
                var itemType = "SP.Data." + listName.charAt(0).toUpperCase() + listName.slice(1) + "ListName";
                var encItemType = itemType.replace(/\s/g,'_x0020_');
                return(encItemType);
    }       
        function getFormDigest(siteurl) {
    
            return $.ajax({
    
                url: "https://siteurl.sharepoint.com/sites/Projects/USMC/AMMO/_api/contextInfo",
    
                method: 'POST',
    
                headers: {
                    'Accept': 'application/json; odata=verbose'
                }
    
            });
            }   
</script>

I hope someone can help! This has been driving me insane!

Replies

This discussion has been closed.