0

It shows undefined in the alert()..

    $.ajax({
      // url: "https://sharepoint.com/sites/FE/_api/web/lists/GetByTitle('Features')/Items",
      url: "https://sharepoint.com/sites/FE/_api/web/lists/GetByTitle('Features')/getitembyid('"+$id+"')",
        type: "GET",     
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {

            alert("success");
alert(data.d.results);
        },
        error: function (error) {
         alert("error");
         alert($id);

        }
    });

With second url, it shows the alert success but it is not fetching items by id.

2

Use alert(data.d) instead of alert(data.d.results).

_api/web/lists/GetByTitle('Features')/getitembyid('"+$id+"')

Above endpoint means, you are requesting for a single item. For multiple items, endpoint will be

_api/web/lists/GetByTitle('Features')/Items

And items will be in data.results.

Difference:

data.results is for multiple items

data.d is for single item

0
$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl+"/_api/Web/Lists/GetByTitle('Features')/GetItemById(2)",
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function (data) {
        console.log(data.d);
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});
  • It takes GetItemById('9') or GetItemById(9) but does not take GetItemById("9"). In OP's question output will be something like GetItemById('9'). – Atish Dipongkor - MVP Nov 13 '15 at 6:45
  • Yes ID needed to be passed as Int ad also data.d should be used instead of data.d.results Two errors needed to be rectified in above code to make it work. – Ankitkumar Malde Nov 13 '15 at 6:45
  • I can see that there is only one error. Please see my previous comment. @AnkitkumarMalde – Atish Dipongkor - MVP Nov 13 '15 at 6:54

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.