1

When I check console in Chrome, the Sharepoint page behaves as it is supposed to when data is Object {d: Object} and d is an Array of the items of want.

When data is #document, the page does not load as I append html based on data.

I understand #document appears because of jQuery's Intelligent Guess, but am not sure why it is getting returned.

function getItems() {
    var url = hostWebURL + "_api/web/lists('" + guid + "')/items/";
    var items;
    $.ajax({
            url: url,
            type: "GET",
            headers: { "Accept": "application/json;odata=verbose "}, // return data format
            success: function (data) {
                //items is iterable ListItemCollection
                            console.log(data);
                items = data.d.results;
            ...
          },
            error: function (error) {
                var errorMsg = "";

                if (error.status == "403" || error.status == "401") {
                    errorMsg = "You do not have Authorization to see Site Permissions - ErrorCode(" + error.status + ") Error Details: " + error.statusText;
                }
                else {
                    var errorMsg = "Failed - ErrorCode(" + error.status + ") Error Details: " + error.statusText;
                }

                reportError(errorMsg);
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Andreas Jun 13 '17 at 15:20
  • 1
    document is probably from an error page, the json is from a success – dandavis Jun 13 '17 at 15:22
  • Original post edited with what happens on error. That can't be it can it? Also am currently reading post @Andreas linked – Nathan Yuchi Jun 13 '17 at 15:27
  • Small follow-up question: Is the code within the success function also asynchronous relative to itself? – Nathan Yuchi Jun 13 '17 at 15:36

1 Answers1

1
beforeSend: function (XMLHttpRequest) {                   
         XMLHttpRequest.setRequestHeader("Accept", "application/json; odata=verbose");

Added this parameter to the call and it's working!

Taken from: http://ratsubsharewall.blogspot.com/2017/02/rest-call-returns-xml-instead-of-json.html