1
var endPointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbyTitle('c2Dashboard Data')/items";

var digest = document.querySelector('#__REQUESTDIGEST').value;
console.log(digest);
var axiosConfig =  {
                  headers: {  
                        "Accept": "application/json;odata=verbose",                     
                        "X-RequestDigest": digest
                  }
               }

var postData = { 
              Permanent: '0', 
              Itinerant: '0', 
              Region: 'Western', 
              Service: 'Army' 
           }

axios.post(endPointUrl, postData, axiosConfig)
               .then(function(response){
                        console.log('saved successfully')
                    })
               .catch(function(error) {
                        console.log(error);
                    });     

I get the following error about 'val' but there is no 'val' just 'value'

'Promise' is undefined File: axios.min.js, Line: 8, Column: 401

  • the problem isn't with your code, it's with your setup. – Derek Gusoff Jul 24 '18 at 12:01
  • What do you mean? – OLA Jul 24 '18 at 16:51
  • where does this code live? How's it deployed? How are you referencing your dependencies? – Derek Gusoff Jul 25 '18 at 12:26
  • The code is in SharePoint and used in a content editor webpart. The dependencies are referenced in an html file where my js file withe the axios code is referenced also. – OLA Jul 26 '18 at 14:04
0

Try using below code to POST your data in SharePoint list:

$.ajax({  
     async: true, 
     url: endPointUrl,  
     method: "POST",
     data: JSON.stringify({  
         '__metadata': {  
             'type': 'SP.Data.ListItem' // it defines the ListEntityTypeName  
         },
         Permanent: '0', 
         Itinerant: '0', 
         Region: 'Western', 
         Service: 'Army' 
     }),  
     headers: {  
         "accept": "application/json;odata=verbose",
         "content-type": "application/json;odata=verbose",
         "X-RequestDigest": $("#__REQUESTDIGEST").val()
     },  
     success: function(data) {  
         console.log("Item created successfully"); 
     },  
     error: function(error) {  
         console.log(JSON.stringify(error));  

     }        
}) 

Before using this function pass the ListEntityTypeName of your own list in __metadata property.

You can get this property by hitting below url in browser tab:

http://<sitecollection>/<site>/_api/web/lists/getbytitle(listName)/entityTypeName

Sources:

  1. SP.List.entityTypeName property.

  2. CRUD Operations In SharePoint REST API Using jQuery AJAX.

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.