我确实设法让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 - 即使我有完全控制也没有权利..答案 0 :(得分: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));
}
});
}
答案 1 :(得分:0)
这里我修改了一些行。请使用 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));
}
})
}