Two things:
- Your metadata type is wrong. It should be SP.Data.ChangeOfAddressListListItem. The format is SP.Data.(list name)ListItem or SP.Data.(library name)Item when adding or updating an item.
- You are missing some headers. A POST request signals an add but you want to do an update. To tell SharePoint that you really want to do an update you need to send the X-Http-Method header with a value of MERGE or PATCH. You should also send an IF-MATCH header to tell SharePoint how to do concurrency checking. I'm not sure if this header is optional or not. Finally, you should add a Content-Type header to tell SharePoint the schema of the message body.
Here is the code with these changes applied.
<script>
$('#submitdata').click(function(){
var itemProperties = {
'__metadata': { "type": "SP.Data.ChangeOfAddressListListItem" },
Title: $("#firstName").val(),
PerOrBus: "Yes"
};
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ChangeOfAddressList')/items",
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>
Here's a working example from one of my conference talks:
function updateItem() {
UpdateFormDigest(_spPageContextInfo.webServerRelativeUrl, _spFormDigestRefreshInterval)
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Tasks')/Items/?$top=1",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data, textStatus, jqXHR) {
var items = data.d.results;
if (items.length > 0) {
var item = items[0];
updateItem(item);
}
});
call.fail(failHandler);
function updateItem(item) {
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('Tasks')/Items(" +
item.Id + ")",
type: "POST",
data: JSON.stringify({
"__metadata": { type: "SP.Data.TasksListItem" },
Status: "In Progress",
PercentComplete: 0.10
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"IF-MATCH": item.__metadata.etag,
"X-Http-Method": "PATCH"
}
});
call.done(function (data, textStatus, jqXHR) {
var div = jQuery("#message");
div.text("Item updated");
});
call.fail(failHandler);
}
}
You can download the slide deck and the samples for the talk from http://ow.ly/4mRlUc