Sharepoint REST - Delete item from list - rest

i would like to delete item from list in SharePoint 2016. I use SharePoint REST API, but I cannot successfully delete item. This is error message (http code 400):
A node of type 'EndOfInput' was read from the JSON reader when trying to read the start of an entry.A 'StartObject' node was expected.
Here is my code:
$.ajax({
url: 'https://myshp.com/test/_api/web/lists(guid'e23e21c7-ab29-445e-87b8-2b20b721f79d')/items?$filter=ID eq '5'',
type: 'POST',
contentType: 'application/json;odata=verbose',
headers: {
"ACCEPT": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*",
"X-HTTP-Method-Override": "DELETE"
},
success: function (data) {
console.log(data);
},
error: function (error) {
console.log(error);
}
});
What is incorrect? Thanks

Please use below code: It works fine for me :
$.ajax({
url: "https://myshp.com/test/_api/web/lists(guid'e23e21c7-ab29-445e-87b8-2b20b721f79d')/items(5)",
type: 'POST',
contentType: 'application/json;odata=verbose',
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*",
"X-HTTP-Method": "DELETE"
},
success: function (data) {
console.log(data);
},
error: function (error) {
console.log(error);
}
})

Related

Why does the result of http request return an empty array?

I've tried using axios and fetch to get data from an API endpoint, but turns out to have only returned an empty array. It works fine in Postman. What have I missed?
Using Axios
axios({
method: "get",
url: "/api/purchase/receivegoods/index?action=detail-grn",
params: {
goods_receive_id: 71,
},
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
.then((response) => {
console.log(("response", response));
})
.catch((error) => {
console.log("error message: ", error.message);
});
Using Fetch
fetch(
"https://devactive.com/api/purchase/receivegoods/index?action=detail-grn",
{
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
params: {
goods_receive_id: 71,
},
}
)
.then((response) => response.json())
.then((result) => {
console.log("Success:", result);
})
.catch((error) => {
console.error("Error:", error);
});
EDIT:
The request is in JSON format. It looks like this (this is an example of an existing id:
{
"goods_receive_id" : 71
}
check for possible proxy configuration, postman uses system proxy by default.
You are not passing the data as the body when using Axios or Fetch. You are hitting the endpoint though. Try something like this:
data = {
"goods_receive_id" : 71
}
axios({
method: "get",
url: "/api/purchase/receivegoods/index?action=detail-grn",
data: JSON.stringify(data), // <- You add the data to the body here
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
.then((response) => {
console.log(("response", response));
})
.catch((error) => {
console.log("error message: ", error.message);
});
or
data = {
"goods_receive_id" : 71
}
fetch(
"https://devactive.com/api/purchase/receivegoods/index?action=detail-grn",
{
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
data: JSON.stringify(data), // <- You add the data to the body here
}
)
.then((response) => response.json())
.then((result) => {
console.log("Success:", result);
})
.catch((error) => {
console.error("Error:", error);
});

On executing the function success or error of $.ajax() doesn't execute in sharepoint 2013. I am being using rest api

Heading ##On execution of fetchCode function checkDuplicacy function doesn't execute
Heading ##function fetchCode(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('FeedbackList')/items?$select=EmployeeCode";
$.ajax({
url : requestUri,
type : "GET",
async : "false",
headers : {
"accept" : "application/json; odata=verbose"
},
success : checkDuplicacy,
error : onError
})
}
Alert the error if you not familiar with developer tool debug(insert script into script editor webpart directly).
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript">
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('car model')/items",
type: 'GET',
async: false,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
}
})
.done(function (data, textStatus, jqXHR) {
var count = data.d.results.length;
alert(count);
})
.fail(function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
})
.always(function () {
alert("complete");
});
</script>

Using rest api for fetching data from iserver on SharePoint page

I have one task to fetch the data from iServer to SharePoint page using rest. I am getting the error:
'Failed to load resource: net::ERR_NAME_NOT_RESOLVED'.
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function () {
EventsCalenderfunc();
});
function EventsCalenderfunc() {
$.ajax
({
url: "https://{iserver URL}/_api/web/items",
type: 'GET',
data: JSON,
headers:
{
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*",
"X-HTTP-Method": null
},
cache: false,
success: function (data) {
for (var i = 0; i < data.d.results.length; i++) {
var item = data.d.results[i];
console.log(item);
}
},
error: function (data) {
console.log('Something is wrong');
}
});
}
</script>
In console I am getting the output 'Something is wrong' and the error described above.

api calling error in ionic-framework

I am getting this error:
XMLHttpRequest cannot load http://example.com/user/api?action=user_info. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.122:8100' is therefore not allowed access.
I have adde proxy url as show below:
{
"name": "conference",
"app_id": "",
"proxies": [
{
"path": "/api",
"proxyUrl": "http://example.com/user/api"
}
]
}
Calling api:
$http({
method: 'POST',
url: 'http://example.com/user/api?action=user_info',
headers: {'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(data, status) {
console.log('Got some data: ', data);
}).error(function(data, status) {
console.log('Got some error: ', data);
console.log('Got some error: ', status);
});
There is little mistake:
remove full url as show in below code:
$http({
method: 'POST',
url: '/api?action=login',
headers: {'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(data, status) {
console.log('Got some data: ', data);
}).error(function(data, status) {
console.log('Got some error: ', data);
console.log('Got some error: ', status);
});

Update list item using SharePoint REST API not working

I am facing issues with updating a simple list item using SharePoint REST API. I have gone through all the blogs to get a solution but the result is same. When ever i execute the update list item function using REST API it returns me the body of the particular list item row i am trying to update but it is not updating the listem. Could someone please help me out. Thanks in advance
function (listTitle, TabId, success, failure) {
var itemType = GetItemTypeForListName(listTitle);
var query = appweburl + "_api/SP.AppContextSite(#target)/web/lists/getbytitle('" + listTitle + "')/items(5)?&#target='" + hostweburl + "'";
var meta_data = {"__metadata": { "type": itemType }};
meta_data['DeletedStatus'] = 'Inactive'
var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: query,
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(meta_data),
//body: meta_data,
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"IF-MATCH": "*"
//"content-length": meta_data.length,
},
success: function (data) {
//alert("success: " + JSON.stringify(data));
//deferred.resolve(JSON.parse(data.body));
console.log(JSON.stringify(data));
alert(JSON.stringify(data));
},
error: function (err) {
//alert("error: " + JSON.stringify(err));
console.log(JSON.stringify(err));
}
});
}
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}
Otherwise just replace below code in your function
executor.executeAsync({
url : query,
method : "POST",
body: JSON.stringify(meta_data),
headers: {
"Accept": "application/json;odata=verbose",
"Content-Type" : "application/json;odata=verbose",
"X-HTTP-Method": "MERGE",
"IF-MATCH": "*"
},
success: function (data) {
console.log(JSON.stringify(data));
},
error: function (err) {
console.log(JSON.stringify(err));
} });
It will also insert the digest for you, so you do not need add
"X-RequestDigest": $("#__REQUESTDIGEST").val(),