Pergunta

I am having issues adding a new list item to SP. I am making an XMLHttpRequest, however I am getting the following message:

"message":{"lang":"en-US","value":"The HTTP header Content-Type is missing or its value is invalid."},

I am not sure why, if id do it with jquery ajax I get the following:

The maximum number of bytes allowed to be read from the stream has been exceeded. After the last read operation, a total of 1049600 bytes has been read from the stream; however a maximum of 1048576 bytes is allowed.

The string that I am sending to SP is 1,165,755 long.

Ajax Call :

            var sniffer = contentObjectforSPOL.Content.length;

            var item = {
                "__metadata": { "type": "SP.Data.SPsiteNameListItem" },
                "Title": contentObjectforSPOL.Title,
                "CTCDescription": contentObjectforSPOL.Description,
                "CTCContentKeyWords": contentObjectforSPOL.KeyWord,
                "CTCSummary": contentObjectforSPOL.Summary,
                "CTCXML": contentObjectforSPOL.Content,
                "CTCApproved": "Pending",
            };



      $.ajax({
                url: "https://tenant.com/sites/SPsiteName/_api/web/lists/getbytitle('listName')/items",
                type: "POST",
                contentType: "application/json;odata=verbose",
                data: JSON.stringify(item),
                headers: {
                    "Authorization": "Bearer " + token,
                    "Accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },
                success: function (data) {
                    FeedBackMessage("Successfully Submitted");
                    clearForm();
                },
                error: function (data) {
                    MessageWindow();
                }
            });

XMLHttpRequest:

            var sniffer = contentObjectforSPOL.Content.length;

            var item = {
                "__metadata": { "type": "SP.Data.SPsiteNameListItem" },
                "Title": contentObjectforSPOL.Title,
                "CTCDescription": contentObjectforSPOL.Description,
                "CTCContentKeyWords": contentObjectforSPOL.KeyWord,
                "CTCSummary": contentObjectforSPOL.Summary,
                "CTCXML": contentObjectforSPOL.Content,
                "CTCApproved": "Pending",
            };

            var url = "https://tenant.com/sites/SPsiteName/_api/web/lists/getbytitle('ListName')/items";
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open('POST', url, false);
            xmlhttp.setRequestHeader("Authorization", "Bearer " + token);
            xmlhttp.setRequestHeader("Accept", "application/json;odata=verbose");
            xmlhttp.setRequestHeader("X-RequestDigest", $("#__REQUESTDIGEST").val());
            xmlhttp.responseType = 'application/json;odata=verbose';
            xmlhttp.onloadend = function (e) {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    var stop = 0;
                }
                else {
                    errorResults.Location = url;
                    errorResults.ErrorCode = xmlhttp.status;
                    errorResults.ErrorResponseText = xmlhttp.statusText;
                    ErrorHandler(errorResults);
                }
            };
            xmlhttp.onerror = function (error) {
                MessageWindow();
            };
            xmlhttp.send(item);
Foi útil?

Solução

You need to specify content-Type in headers not as $.ajax property. I mean

headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose"
        }

Modified Code

    $.ajax({
    url: "https://tenant.com/sites/SPsiteName/_api/web/lists/getbytitle('listName')/items",
    type: "POST",
    data: JSON.stringify(item),
    headers: {
        "Authorization": "Bearer " + token,
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "content-Type": "application/json;odata=verbose"
    },
    success: function(data) {
        FeedBackMessage("Successfully Submitted");
        clearForm();
    },
    error: function(data) {
        MessageWindow();
    }
});

Also read more about adding item in my article.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top