0

I am able to read list items using REST but when attempting to add to list I get "Uncaught TypeError: '#__REQUESTDIGEST".val is not a function.

js:

    jQuery.ajax({
        url: "https://.../_api/web/lists/GetByTitle('EU34SupportTracker')/items&$expand=AttachmentFiles",
        method: "POST",
        data: JSON.stringify({
            '__metadata' : {'type': 'SP.Data.EU34SupportTrackerListItem'},
            'Title': title
        }),
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": formdigest
        },
        success: function(data) {
            console.log("entry created successfully...");
        },
        error: function(data) {
            console.log(("#__REQUESTDIGEST").val()); //ERRORS RIGHT HERE
            console.log("Error message: " + JSON.stringify(data.responseJSON.error));
        }

I am able to read all the items just fine. Complete code is below. All of my code is part of an angularjs app. Reading the list items works error happens when attempting to add to the list using REST.

var app = angular.module('myApp', []);

app.controller('supportController',
    function($scope, $http) {

        // Get current user
        jQuery.ajax({
            url: "https://.../_api/web/currentuser?$select=Title",
            type: "GET",
            headers: {"Accept": "application/json;odata=verbose","Content-Type": "application/json;odata=verbose"},
            success: function(data) {
                username = data.d.Title;
                console.log("user name: " + username);
                document.getElementById("user").innerHTML = username;
            },
            error: function(data) {
                console.log("Error occurred trying to get user id");
            }
        });
        // Create required form digest 
        $scope.getFormDigest = function() {
            console.log("Inside getFormDigest");
            var formdigest;

            jQuery.ajax({
                url: "https://.../_api/contextinfo",
                type: "POST",
                async: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    type: "POST"
                },
                success: function(data)
                {   
                    console.log(typeof data);
                    formdigest = data.d.GetContextWebInformation.FormDigestValue
                }
            });
            return formdigest;
        };
        $scope.updateList = function() {

            console.log("Inside updateList...");
            const dt = new Date();
            const title = document.getElementById("title").value;
            const network = document.getElementById("network").value;
            const enclave = document.getElementById("enclave").value;
            const application = document.getElementById("application").value;
            const sysuseracct = document.getElementById("sysuseracct").value;
            const problemtype = document.getElementById("problemtype").value;
            const dateissuebegan = document.getElementById("dateissuebegan").value;
            const details = document.getElementById("details").value;
            const pocname = document.getElementById("pocname").value;
            const pocnumber = document.getElementById("pocnumber").value; 
            const pocemail = document.getElementById("pocemail").value;
            /** attachements will go here */
            const externaltt = document.getElementById("externaltt").value;
            const worklog = document.getElementById("worklog").value;

            var formdigest = $scope.getFormDigest();

            document.getElementById("title").value = "";
            document.getElementById("network").value = "";
            document.getElementById("enclave").value = "";
            document.getElementById("application").value = "";
            document.getElementById("sysuseracct").value = "";
            document.getElementById("problemtype").value = "";
            document.getElementById("dateissuebegan").value = "";
            document.getElementById("details").value = "";
            document.getElementById("pocname").value = "";
            document.getElementById("pocnumber").value = "";
            document.getElementById("pocemail").value = "";
            /** attachements will go here */
            document.getElementById("externaltt").value = "";
            document.getElementById("worklog").value = "";

            console.log(title);
            console.log(network);
            console.log(enclave);
            console.log(application);
            console.log(sysuseracct);
            console.log(problemtype);
            console.log(dateissuebegan);
            console.log(details);
            console.log(pocname);
            console.log(pocnumber);
            console.log(pocemail);
            console.log(externaltt);
            console.log(worklog);
            
            var formdigest = $scope.getFormDigest();

            jQuery.ajax({
                url: "https://.../_api/web/lists/GetByTitle('EU34SupportTracker')/items&$expand=AttachmentFiles",
                method: "POST",
                data: JSON.stringify({
                    '__metadata' : {'type': 'SP.Data.EU34SupportTrackerListItem'},
                    'Title': title
                }),
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                    "X-RequestDigest": formdigest
                },
                success: function(data) {
                    console.log("entry created successfully...");
                },
                error: function(data) {
                    console.log(("#__REQUESTDIGEST").val());
                    console.log("Error message: " + JSON.stringify(data.responseJSON.error));
                }
            });
            // close modal
            $('#myModal').modal('hide');
        };

        $http({
            method: 'GET',
            url: "https://.../_api/web/lists/GetByTitle('EU34SupportTracker')/items?$select=Statue,Title,Network,Enclave,Application,User_x0020_Account,Problem_x0020_Type,DateCreated,Details,POC_x0020_Name,POC_x0020_Phone_x0020_Number,Attachments,AttachmentFiles,External_x0020_Ticket_x0023_,Worklog&$expand=AttachmentFiles",
            headers: {"Accept": "application/json;odata=verbose"}
        }).then(function (data, status, headers, config) {
            $scope.tickets = data.data.d.results;
            console.log($scope.tickets);
            //$scope.tickets.map(ticket => console.log(ticket));
            $scope.tickets.map(ticket => { 
                console.log(ticket.AttachmentFiles.results[0].FileName);
                console.log(ticket.AttachmentFiles.results[0].ServerRelativeUrl);
                console.log(ticket);
            });

        }, function errorCallback(response) {
            console.log(response);
        });
});

1 Answer 1

1

Shouldn't it be console.log(jQuery("#__REQUESTDIGEST").val()); instead of console.log(("#__REQUESTDIGEST").val());?

1
  • Sandro, you are correct, but it would be helpful if you edited your answer to add a bit of explanation as to why that is the case. Oct 2, 2020 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.