0

Trying to create a list item in SharePoint using REST, getting the following error message:

Uncaught TypeError: Converting circular structure to JSON

Am I missing something obvious here?

$.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists('" + myList + "')/items",
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        data: JSON.stringify({
            'Title': 'testing',
            '__metadata': { 'type': 'SP.Data.List_x005f_TasksListItem' }
        }),
        success: function (data) {
            TX = data.d.Id;
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    });
2

User the following function to Create ListItem in SharePoint List

function CreateListItem(myList) {
var listMetadataType = "SP.Data." + myList + "ListItem"
var listNameInternal = GetListItemEntityTypeFullName(_spPageContextInfo.webAbsoluteUrl, myList);
listNameInternal.done(function(data) {
    listMetadataType = data.d.ListItemEntityTypeFullName;
});

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + myList + "')/Items",
    type: "POST",
    headers: {
        "accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    data: JSON.stringify({
        '__metadata': { 'type': listMetadataType },
        'Title': 'testing',
    }),
    success: function(data) {
        TX = data.d.Id;
    },
    error: function(error) {
        alert(JSON.stringify(error));
    }
});}

function GetListItemEntityTypeFullName(siteUrl, listTitle) {
    return $.ajax({
    url: siteUrl + "/_api/web/lists/GetByTitle('" + listTitle + "')?$select=ListItemEntityTypeFullName",
    method: "GET",
    async: false,
    headers: {
        "Accept": "application/json; odata=verbose"
    }
});}
0

To add list item to list using REST API with jQuery code, the following code for your reference.

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
var listName="CustomList";    
function AddListItem(){
    var itemType = GetItemTypeForListName(listName);
    var item = {
        "__metadata": { "type": itemType },
        "Title": "testing"
    };   
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_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) {
            alert('Success');            
        },
        error: function (data) {
             alert(JSON.stringify(error));
        }
    });
}
// Get List Item Type metadata
function GetItemTypeForListName(name) {
    return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}  
</script>
<input type="button" onclick="AddListItem()" value="AddListItem"/>
0

If your list name is "List_Tasks" then everything is fine in your code, you just need to add "/getbytitle" in your url after /lists.

url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + myList + "')/items",

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.