I created a blank form on a HTML webpage through my Sharepoint site called http://.../testform.html
.
<form>
<input type='text' id='name'>
<input type='submit' id='submitdata' value='submit'><br>
</form>
and I'm trying to have the input submitted to a SharePoint list called Test
.
So I came up with the below code and I tested it using the Script Editor Web Part
and it worked.
<script type="text/javascript">
$(document).ready(function() {
$('#submitdata').click(function() {
var name = $('#name').val();
var list = "Test";
addListItem(name, list);
});
});
function addListItem(name, listname) {
var listType = "Custom List";
var item = {'__metadata': {'type': 'SP.Data.TestListItem'},
'Title': name
}
var siteUrl = "https://intelshare.intelink.gov/sites/carm"
$.ajax({
url: siteUrl + "/_api/web/lists/getByTitle('" + listname + "')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
success: function(data) {
console.log("done");
},
error: function(err) {
console.log(JSON.stringify(err));
}
});
}
</script>
However when I use the same code from the Script Editor webpart
in the html page I get the error below:
The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again
i loaded
<script type="text/javascript" src="./jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="./jquery.SPServices.min.js"></script>
I tried looking the error up and I came up with calling data.d.GetContextWebInformation.FormDigestValue
but do not know how to incorporate it as it errors out. I am new to java and need a budge on the right direction.