I am having difficulties with the JQuery implementation of AJAX to make REST calls to POST data to add items to my SharePoint list. Using 'GET' to retrieve information gives me no issues. Given the following form header information:
<form name="frmRequestNewPDO" autocomplete="off" runat="server" method="post">
<SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>
<div>
<!-- Form content goes here -->
</div>
<div>
<input type="submit" id="btnSubmit" value="Submit Data">
</div>
</form>
I am using the following AJAX function to post my data:
function submitListItems(listName, jsonObj, _async) {
var jsonObj.__metadata = {'type': GetItemTypeForListName(listName)};
// Update form digest for X-Request-Digest
UpdateFormDigest(_spPageContextInfo.webServerRelativeUrl, _spFormDigestRefreshInterval);
var baseRequest = {
url: "",
type: "",
contentType: "",
data: ""
};
var request = baseRequest;
request.type = 'POST';
request.contentType = 'application/json;odata=verbose';
request.data = JSON.stringify(jsonObj);
request.url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items";
request.headers = {
ACCEPT: 'application/json;odata=verbose',
'X-RequestDigest': $("#__REQUESTDIGEST").val()
};
return $.ajax(request);
};
This is constantly going to the $.ajax.fail() Promise method instead of the $.ajax.done() Promise method. When I look at the SharePoint list that it is sending data to, the data is written correctly. However, when I look at the console output, it gives me the error:
{"readyState":0,"status":0,"statusText":"error"}
Originally I had read it could be due to domain cross-posting, but given that it is a SharePoint-hosted app, I don't see how this could be the case. I tried adding the line
UpdateFormDigest(_spPageContextInfo.webServerRelativeUrl, _spFormDigestRefreshInterval);
but this did not solve the issue. Does anyone have any suggestions?
responseText. It will give us more idea about error