Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Ajax

Ajax: Add Query Parameters to API Requests

- 20.06.26 - ErcanOPAK

🔗 ?page=1&limit=10 — Query Parameters

API requests need parameters. URLSearchParams builds query strings. Clean, safe, automatic encoding.

📝 Using URLSearchParams

// Basic usage
const params = new URLSearchParams();
params.append('page', '1');
params.append('limit', '10');
params.append('search', 'javascript');

const url = `/api/users?${params}`;
// /api/users?page=1&limit=10&search=javascript

// With object
const query = { page: 1, limit: 10, search: 'javascript' };
const params = new URLSearchParams(query);
const url = `/api/users?${params}`;

// From existing URL
const url = new URL('https://api.example.com/users');
url.searchParams.set('page', '1');
url.searchParams.set('limit', '10');
const fullUrl = url.toString();

// Get values
const page = url.searchParams.get('page');  // '1'
const limit = url.searchParams.get('limit');  // '10'
const hasLimit = url.searchParams.has('limit');  // true
const allParams = url.searchParams.getAll('filter');  // ['value1', 'value2']

🎯 Real-World Examples

// Pagination
function fetchUsers(page = 1, limit = 20) {
  const params = new URLSearchParams({ page, limit });
  return fetch(`/api/users?${params}`);
}

// Filters
function fetchProducts(filters) {
  const params = new URLSearchParams(filters);
  return fetch(`/api/products?${params}`);
}

// Search with multiple filters
const searchParams = {
  q: 'laptop',
  brand: 'apple',
  minPrice: 500,
  maxPrice: 2000,
  sort: 'price_asc'
};
const params = new URLSearchParams(searchParams);
const url = `/api/search?${params}`;

// Array parameters
params.append('tags[]', 'electronics');
params.append('tags[]', 'new');
// tags[]=electronics&tags[]=new

💡 Benefits

  • Auto-encoding (spaces become %20)
  • Clean syntax
  • Works with arrays and objects
  • Immutable URL creation
  • Browser native (no library needed)

“Manual query strings were error-prone. URLSearchParams handles encoding. Clean, safe, easy. Essential for API calls.”

— API Developer

Related posts:

Ajax Responses Arrive Out of Order

Ajax Responses Arrive Out of Order

Ajax: Understand CORS for Cross-Origin Requests

Ajax: Understand CORS for Cross-Origin Requests

Ajax: Understand REST API HTTP Methods (GET, POST, PUT, DELETE)

Ajax: Understand REST API HTTP Methods (GET, POST, PUT, DELETE)

Views: 13
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

JavaScript: Understand Closures for Powerful Functions
Git: Use Branches to Work on Features Independently

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com