首页 > 解决方案 > 共享点中的 REST POST 不发布数据

问题描述

我确实设法让 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 - 即使我拥有完全控制权,也没有权利..

标签: jqueryrestpostsharepoint

解决方案


关于为什么请求不起作用,我注意到以下几点。执行 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));  
        }  
    });
}

推荐阅读