0

So I have a snippet of code that will update a field value if the field has content, although if the field that I'm trying to update is null than the value won't update. Am I doing something wrong?

siteURL = _spPageContextInfo.webAbsoluteUrl;
        var apiPath = _spPageContextInfo.webAbsoluteUrl +"/_api/lists/getbytitle('Training%20Copy')/items/getbyid(9)"; 
        $.ajax({  
                url: apiPath,  
                type: "POST",  
                headers: {  
                    Accept: "application/json;odata=verbose"  
                },  
                data: JSON.stringify
                ({
                    __metadata:
                    {
                        type: "SP.Data.Training_x0020_CopyItem"
                    },
                    Admin_x0020_Function: "Have content"
                    
                }), 
                headers: {  
                "Accept": "application/json;odata=verbose",
                "Content-Type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "IF-MATCH": "*",  
                "X-HTTP-Method": "MERGE" 
            }, 
            async: false, success: function(data) {  
                console.log("Item updated successfully");  
            }, eror: function(data) {  
                console.log("An error occurred. Please try again.");  
            }
        }) 
user2602844
  • 133
  • 1
  • 6

1 Answers1

0

There are two headers parameters in your ajax request, it is not clear whether it will affect.

My test code for your reference:

  <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script>
    $(document).ready(function () {
    Update()
    function Update(){  
        $.ajax({  
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Doc')/items(9)",  
        type: "POST",  
        headers: {  
            "accept": "application/json;odata=verbose",  
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
            "content-Type": "application/json;odata=verbose",  
            "IF-MATCH": "*",  
            "X-HTTP-Method": "MERGE"  
        },  
        data: JSON.stringify({__metadata:{'type':'SP.Data.DocItem'},test:'test'}),  
        /*where Title is column name and add your desired new data*/  
        success: function(data) {  
            console.log(data);  
        },  
        error: function(error) {  
            alert(JSON.stringify(error));  
        }  
    });
    }
    })
    </script>
Amos
  • 2,030
  • 1
  • 5
  • 9