Reputation: 1
Here I am trying to update the SharePoint list item using the SPHttps client, It is giving me the 403 forbidden error but I'm the global administrator of that site and have full control on the list. this is my code:
handleFormUpdate = async (userdata: IContact): Promise<void> => {
const uniqueID = userdata.UniqueID0;
try {
const response = await fetch(
`https://{mysiteurl}/_api/web/lists/getbyTitle('AddressBook')/items?$filter=UniqueID0 eq '${uniqueID}'&$select=ID,UniqueID0`,
{
method: "GET",
headers: {
Accept: "application/json;odata=verbose",
},
}
);
const data = await response.json();
if (data.d && data.d.results && data.d.results.length > 0) {
const item = data.d.results[0];
if (item.ID) {
console.log(item.ID);
await this.updateItem(item.ID, userdata);
} else {
console.error("ID field missing in response:", item);
}
} else {
console.error("Item not found with UniqueID:", uniqueID);
alert("Item not found with UniqueID: " + uniqueID);
}
} catch (error) {
console.error("Error fetching item:", error);
alert("Error fetching the item: " + error.message);
}
};
updateItem = async (itemID: number, userdata: IContact): Promise<void> => {
try {
const response = await fetch(
`https:/{mysiteurl}/_api/web/lists/getbyTitle('AddressBook')/items(${itemID})`,
{
method: "POST",
headers: {
Accept: "application/json;odata=nometadata",
"Content-Type": "application/json;odata=verbose",
"X-HTTP-Method": "MERGE",
"odata-version": "",
},
body: JSON.stringify({
FullName: userdata.FullName,
Email: userdata.Email,
UniqueID0: userdata.UniqueID0,
Mobile: userdata.Mobile,
Website: userdata.Website,
Address: userdata.Address,
Landline: userdata.Landline,
}),
}
);
if (!response.ok) {
const errorDetails = await response.json();
console.error("Error details:", errorDetails);
alert("Update failed: " + errorDetails.error.message);
}
if (response.ok) {
console.log("Update successful");
alert("Contact updated successfully!");
this._getListItems();
} else {
console.error("Update failed:", response.statusText);
alert("Failed to update contact. Check the console for details.");
}
} catch (error) {
console.error("Error updating item:", error);
alert("Error updating the item: " + error.message);
}
};
in console i see this odata error : "The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."
How can i resolve this 403 forbidden and odata.error
Upvotes: 0
Views: 14