2

I using Sharepoint 2013 I want to get list items using Sharepoint 2013 REST feature

I can get all items but when i put my filter it does not works I don't know where the problem is.

my code:

 <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<input type="text" name="txtbox1" id="txtbox1" />
<input type="button" value="test it works" onclick="load_sls($('#txtbox1').val());" />
<script type="text/javascript">
function load_sls(province)
{
    var call =$.ajax({
    url: "http://server/sites/test/_api/web/lists/GetByTitle('Agency')/items? $select=Id,Title?$filter=[Title eq '"+province+"']",
    type: "GET",
    data: JSON,
    headers: { 

        "accept": "application/json;odata=verbose",

    }
    //success: doSuccess,
    //error: doError
 });
call.done(function(data,textStatus,jqXHR){
    alert('success'+jqXHR.responseText);
});
call.fail(function(jqXHR,textStatus,errorThrown){
    alert('Error occured'+jqXHR.responseText);
});
}
</script>

2 Answers 2

2

The endpoint in your example contains some typos:

  1. Replace ?$filter into &$filter
  2. Remove [ and ] symbols from filter query option since they are not supported

The fixed url:

url: "http://server/sites/test/_api/web/lists/GetByTitle('Agency')/items?$select=Id,Title&$filter=Title eq '"+province+"'"

Further improvements

  1. Use encodeURIComponent function to escape characters, for example:

    var url = webUrl + "/_api/web/lists/GetByTitle('Pages')/items?$select=Id,Title&$filter=Title eq '" + encodeURIComponent(pageTitle) + "'";
    
  2. Prefer jQuery.getJSON() over jQuery.ajax() to perform read operation, for example:

    var getListItems =  function (webUrl,listTitle) {
        var url = webUrl + "/_api/web/lists/GetByTitle('" + listTitle + "')/items";
        return $.getJSON(url,function( data ) {
               return data; 
        });
    }
    
1
0

Try changing the '?' before the $filter to a '&'

To something like this:

http://server/sites/test/_api/web/lists/GetByTitle('Agency')/items?$select=Id,Title&$filter=[Title eq '"+province+"']"

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.