Select2 ajax load data only once and search isn't working

Hi,
I am using select2 to retrieve data from remote data source using S2 ajax but the problem is that data is loaded only once so when i search for another term it doesn’t connect to the server and only searches in the old data.
Here is my Api


[HttpGet]
[Route(“GetItemsWithParam/{term}”)]
public IHttpActionResult GetItemsWithParam(string term)
{
try
{
using (var ctx = new GeneralClasses.AlraedContext())
{
//page *= 10;
var lst = ctx.Items.Where(c => c.Name.Contains(term)).ToList();
return Ok(JsonConvert.SerializeObject(lst));
}
//var lst = Item.GetAll();
}
catch (Exception)
{
return InternalServerError();
}
}


and the select2 ajax

I’m just guessing here, but I’m wondering if the ajax.url value is cached the first time Select2 makes the AJAX request. You are embedding the search term in the URL path, rather than as a query parameter (although it seems you’re also including it there via your ajax.data callback—which seems superfluous since Select2 already provides a query string in that format).

Can you change your server-side code to get the search term from the request’s query string (q parameter) rather than from the URL path?

John30013
Thanks for your reply.
the problem was in the parameters format, i was sending it with incorrect way, now it works perfectly. thanks again

1 Like

@alhammami Can you explain what was wrong?