0

My script:
I have got a list that I want to populate with a HTML form. I'm using the following but I keep getting ID is not defined in google debugger.

<script>
    $('#submitdata').click(function(){

    var itemProperties = {
        '__metadata': { "type": "SP.Data.ChangeOfAddressListListItem" },
        Title: $("#firstName").val(),
        PerOrBus: "True"

    };



    $.ajax({
         url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ChangeOfAddressList')/items(" + id + ")",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(itemProperties),
        headers: {
            "Accept": "application/json;odata=verbose",
            "Content-Type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "IF-MATCH": "*",
            "X-Http-Method": "PATCH"
        },
        success: function (data) {
            console.log(JSON.stringify(data.d));
        },
        error: function (data) {
            console.log(data.responseText);
        }
    });
});


</script>     

It is definitely connected to the list alright

1 Answer 1

1

You have used "id" variable in your code, but where did you define/assign it.

url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ChangeOfAddressList')/items(" + id + ")
5
  • Should I put it in the metadata?
    – Stacker
    May 6, 2016 at 9:49
  • id is auto generated on the list side, so I thought I wouldn't need it
    – Stacker
    May 6, 2016 at 9:52
  • id is auto generated when you are creating a new item. For this request form your REST API url without mentioning the ID. url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ChangeOfAddressList')/items However if you want edit an existing item you need to pass the corresponding List Item ID. url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ChangeOfAddressList')/items(" + id + ")
    – Karvannan
    May 6, 2016 at 10:01
  • ah yes, that is how I had it originally and it didn't work that way either.
    – Stacker
    May 6, 2016 at 10:18
  • If you are trying to create a new list item then remove "IF-MATCH" and "X-HTTP-METHOD" entries from your request headers. headers: { "Accept": "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), }
    – Karvannan
    May 6, 2016 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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