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

4
  • the problem isn't with your code, it's with your setup. Jul 24, 2018 at 12:01
  • What do you mean?
    – OLA
    Jul 24, 2018 at 16:51
  • where does this code live? How's it deployed? How are you referencing your dependencies? Jul 25, 2018 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, 2018 at 14:04

3 Answers 3

1

This is how to update list items

var getData =({});
var data = {
    Title: "1",
    FieldName1: "FieldName1 Value",
    FieldName2: "FieldName2 Value",
    FieldName3: "FieldName3 Value",
}
for(var d in data){
    getData[d] = getData[d];  
}
getData.__metadata ={
    type:  "SP.Data.MylistTitleListItem"
}
console.log('getData');
console.log(getData);   
var digest = document.getElementById("__REQUESTDIGEST").value;   
//We get list item by Id 19:
var endPointUrl = _spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/GetByTitle('MylistTitle')/items(19)";
var axiosConfig =  {
    headers:{
        "X-HTTP-Method": "MERGE",
        "X-RequestDigest": digest,
        Accept: "application/json;odata=verbose",
        "IF-MATCH": "*",
        "Content-Type": "application/json;odata=verbose"                
    }                     
}   
axios.post(endPointUrl, getData, axiosConfig)
    .then(function (response) {
        console.log('Response');
        console.log(response);                   
    })
    .catch(function (error) {
        // handle error https://sharepoint.stackexchange.com/questions/245803/how-to-axios-post-to-sharepoint-2013-list
        console.log(error);
})
1

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

OR

http://<sitecollection>/<site>/_api/web/lists/getbytitle(listName)?$select=ListItemEntityTypeFullName

Sources:

  1. SP.List.entityTypeName property.

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

1
0

THIS WORKED FOR ME: Saving only.

var digest = document.getElementById("__REQUESTDIGEST").value;
    var query = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('mylist title')/items";       
    var axiosConfig =  {
        headers: {
            "X-RequestDigest": digest,
            Accept: "application/json;odata=verbose"
        },
        "Content-Type": "application/json;odata=verbose"
    }

    axios.post(query, _this.Model, axiosConfig)
        .then(function (response) {
            console.log('Savede Data');
            console.log(response.data);                   
        })
        .catch(function (error) {
            // handle error https://sharepoint.stackexchange.com/questions/245803/how-to-axios-post-to-sharepoint-2013-list
            console.log(error);
        });
1
  • what is _this.Model? Where did it come from?
    – OLA
    Dec 13, 2019 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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