1

I have a list called "Users" and I need to retrieve the item count using .ajax from that list. For example, where "FirstName" == "James".

This is my request:

$.ajax({
            url: siteUrl + "/_vti_bin/ListData.svc/Users/$count?$filter=((FirstName eq 'James'))",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                console.log(data);              
            },
            error: function (data) {
                console.log(data);
            }
        });

This way I get an error "Unsupported Media Type".

What am I doing wrong?

2 Answers 2

4

The problem is $count return a plain text not a json so you call should be change from:

headers: { "Accept": "application/json; odata=verbose" }

To

headers: { "Accept": "text/plain;odata=verbose" }

A full example:

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_vti_bin/ListData.svc/MuroRespuesta/$count?$filter=((PreguntaID eq '5'))",
        method: "GET",
        headers: { "Accept": "text/plain;odata=verbose" },
        success: function (data) {
            console.log(data);              
        },
        error: function (data) {
            console.log(data);
        }
    });

enter image description here

Hope be Helpful

2
  • Thanks, @Marco, this solution is a lot faster than mine :)
    – Gintas K
    Dec 1, 2016 at 16:49
  • I'm glad to have helped you :)
    – Marco
    Dec 1, 2016 at 16:53
1

Since you are using SharePoint online, try this REST endpoint. Try below code:

$.ajax({
            url: siteUrl + "/_api/web/lists/getbytitle('Users')/items?$filter=FirstName eq 'James'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                console.log(data.d.results.length);              
            },
            error: function (data) {
                console.log(data);
            }
        });
0

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.