【问题标题】:Update empty field to contain value - rest API更新空字段以包含值 - 休息 API
【发布时间】:2021-02-10 22:40:11
【问题描述】:

所以我有一个 sn-p 代码,如果字段有内容,它将更新字段值,尽管如果我尝试更新的字段为空,则该值不会更新。我做错了吗?

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.");  
            }
        }) 

【问题讨论】:

    标签: sharepoint-2013 sharepoint-rest-api


    【解决方案1】:

    你的ajax请求中有两个headers参数,不清楚会不会影响。

    我的测试代码供你参考:

      <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>
    

    【讨论】:

      猜你喜欢
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      相关资源
      最近更新 更多