1

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?

  • Once I was having the similar issue on our sharepoint site. POST method to update existing list items was throwing an error in IE but was working perfectly in chrome. – Ganesh Sanap Nov 28 '18 at 3:23
  • I was using Chrome to develop and debug... How did you solve the issue on your SharePoint site? – BWieland86 Nov 28 '18 at 3:34
  • Can you add rest api error responseText . It will give us more idea about error – Bharat Khunti Nov 28 '18 at 4:22
  • Just figured out how to do that, but it gives me the following in the console: console.log("dfdList1DataFiles.fail()") -> "dfdList1DataFiles.fail()" console.log("ResponseText: " + error.responseText) -> ResponseText: undefined console.log(JSON.stringify(error)) -> {"readyState":0,"status":0,"statusText":"error"} – BWieland86 Nov 28 '18 at 4:46
  • It could be any number of things. Fiddle the HTTP traffic and see what comes back. telerik.com/fiddler – Derek Gusoff Nov 28 '18 at 14:19
0

In the end, the error turned out to not be with the code above, but with SharePoint itself. SharePoint doesn't like list columns to be named starting with double letters, followed by a number, such as SS1, SS2, SS3, as it thinks they are an Excel cell reference. Eventually I solved it with changing my object members to X_SS1, X_SS2, X_SS3, and this worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.