【问题标题】:REST POST in sharepoint not posting data共享点中的 REST POST 不发布数据
【发布时间】:2018-06-15 17:05:37
【问题描述】:

我确实设法让 GET REST Share 点工作,但我在 POST 方面遇到了困难。

我有这个代码:

function send_idea() {  
    //Fetch the values from the input elements  
    var idea_title = document.getElementById("idea_title").value;
    var idea_description = document.getElementById("idea_description").value;
    var listName = "Production_Request_Idea";

    $.ajax({  
        method: "POST",
        url: "URLTOSITE/_api/web/lists/GetByTitle('Production_Request_Idea')/items", //name of the LIST
        dataType: "json",
        data: {
            Title: idea_title,  //I did try to put Title in "Title" but still not posting
            Description: idea_description 
        },
        headers: {  
            accept: "application/json;odata=verbose", //It defines the Data format   
            contentType: "application/x-www-form-urlencoded" 
        },  
        success: function(data) {  
            swal("Item created successfully", "success"); // Used sweet alert for success message  
        },  
        error: function(error) {  
            console.log(JSON.stringify(error));  
        }  
    })  
}

html:

  <abc runat="server" data-aos="zoom-out" data-aos-once="true" method="post" enctype="text/plain" id="sendIdea">
     <label for="title">Title</label>
     <input type="text" id="idea_title" name="title">
     <label for="idea_description" >Description</label>
     <textarea id="idea_description" name="description"></textarea>
     <p>benefits:</p>
     <div class="benefits_container" >
        <div class="benefit" >
           <input id="quality_container" type="checkbox" name="quality">
           <svg class="benefit_icon svgColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30">
      some vector graphic
           </svg>
           <p>quality</p>
        </div>
        <div class="benefit" >
           <input id="savings_container" type="checkbox" name="savings">
           <svg class="benefit_icon svgColor" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 50 50" version="1.1">
              some vector graphic
           </svg>
           <p>savings</p>
        </div>
        <div class="benefit" id="compliance_container">
           <input id="compliance_container" type="checkbox" name="compliance">
           <svg class="benefit_icon svgColor" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 50 50" version="1.1">
              some vector graphic
           </svg>
           <p>compliance</p>
        </div>
     </div>         
     <button type="submit" onclick="send_idea()">send</button>
  </abc>

它确实会转到标题,然后它会自行刷新而不进一步,所以我认为存在导致浏览器刷新站点的错误。

从昨天开始,我一直在尝试解决这个问题,但无济于事。

我确实在 Stack 上只找到了 2 个示例,但对我没有帮助。

编辑:我将表单标签更改为随机的东西,就像测试一样,但现在当提交绑定到按钮而不是表单时,它至少会向我发送错误消息,即 2130575251 - 即使我有完全控制权也没有权利..

【问题讨论】:

标签: jquery rest post sharepoint


【解决方案1】:

关于为什么请求不起作用,我注意到以下几点。执行 POST 请求时,数据需要以 JSON 格式发送,并且应包含指定类型的元数据对象。此外,还需要包含请求摘要标头。

如果您仍然遇到页面刷新,请添加其余代码。

function send_idea() {  
    //Fetch the values from the input elements  
    var idea_title = $("#idea_title").val();
    var idea_description = $("#idea_description").val();
    var listName = "Production_Request_Idea";

    //Include the metadata object and type
    var data = {
        "__metadata": {
            type: "SP.Data.Production_x005f_Request_x005f_IdeaListItem"
        },
        Title: idea_title,
        Description: idea_description
    };

    $.ajax({  
        method: "POST",
        url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/GetByTitle('Production_Request_Idea')/items", //name of the LIST
        data: JSON.stringify(data),
        headers: {  
            accept: "application/json;odata=verbose", //It defines the Data format   
            "content-type": "application/json;odata=verbose", //Sends as JSON
            "X-RequestDigest": $("#__REQUESTDIGEST").val() //Include request digest value
        },  
        success: function(data) {  
            swal("Item created successfully", "success"); // Used sweet alert for success message  
        },  
        error: function(error) {  
            console.log(JSON.stringify(error));  
        }  
    });
}

【讨论】:

  • 嗨,如果我使用:url:_spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/GetByTitle('Production_Request_Idea')/items",那么网站会自动刷新在这条线之后什么都不做。您认为标准代码对我不起作用的原因有联系吗?我之前确实注意到了这一点,这就是为什么我使用了整个 url,这很好,并且代码在该行之后继续
  • 顺便说一句,即使使用整个 url,我也从未收到任何控制台日志或成功消息。页面刚刚刷新。我不知道如何调试这个..
  • 您能发布其余的代码,包括 HTML 吗?我的猜测是,您可能正在侦听 的 type 属性
  • 你是对的。现在我将操作绑定到按钮操作,但粗略的我有一个不同的问题“System.UnauthorizedAccessException”,即使我可以完全控制自己。无论如何,我想这是一个不同的问题,我会尝试用谷歌搜索它。但该死的,这要花这么长时间。我应该是个园丁。非常感谢。
  • 没问题。你能发布完整的错误信息吗?您是否在请求中添加了“X-RequestDigest”标头?
【解决方案2】:

这里我修改了一些行。请在下面尝试PATCH方法

这对我来说是一个有效的代码。

如有任何问题,请与我联系。

function send_idea() {  
     
    var idea_title = document.getElementById("idea_title").value;
    var idea_description = document.getElementById("idea_description").value;
    var listName = "Production_Request_Idea";
    var item = {
                 "__metadata": { "type": idea_title},
                 "Value": idea_description
     };
    $.ajax({  
        method: "PATCH",
        url: "URLTOSITE/_api/web/lists/GetByTitle('Production_Request_Idea')/items",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
                                "Accept": "application/json; odata=verbose",
                                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                                "If-Match": "*"
        }, 
        success: function(data) {  
            swal("Item created successfully", "success");  
        },  
        error: function(error) {  
            console.log(JSON.stringify(error));  
        }  
    })  
}

【讨论】:

  • 您好,同样的问题。它不做任何事情,页面只是在没有任何日志或诸如此类的情况下自行刷新。无论如何,元数据看起来不对。如果我没记错的话,我认为类型不能是元素的某些值,而是可以通过 API 找到的东西。这就是我找到的“SP.Data.Production_x005f_Request_x005f_IdeaListItem”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多