Skip to content

Bits of .NET

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

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access

Category: Ajax

Ajax

AJAX: Use Fetch with Signal to Cancel Requests When User Navigates Away

- 13.02.26 - ErcanOPAK comment on AJAX: Use Fetch with Signal to Cancel Requests When User Navigates Away

User navigates away but old fetch requests keep running in background. Cancel them properly with AbortController. Setup: const controller = new AbortController(); fetch(‘/api/data’, { signal: controller.signal }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => { if (error.name === ‘AbortError’) { console.log(‘Request cancelled’); } }); // Cancel when user leaves page window.addEventListener(‘beforeunload’, () => { […]

Read More
Ajax

AJAX: Use FormData to Upload Files Without jQuery

- 13.02.26 - ErcanOPAK comment on AJAX: Use FormData to Upload Files Without jQuery

Uploading files with vanilla JavaScript is simpler than you think. FormData handles everything. HTML: <input type=”file” id=”fileInput” multiple> <button onclick=”uploadFiles()”>Upload</button> JavaScript: async function uploadFiles() { const input = document.getElementById(‘fileInput’); const formData = new FormData(); for (let file of input.files) { formData.append(‘files’, file); } const response = await fetch(‘/upload’, { method: ‘POST’, body: formData // Browser […]

Read More
Ajax

Ajax Revolution: How Fetch API and Async/Await Replace jQuery.ajax()

- 05.02.26 - ErcanOPAK comment on Ajax Revolution: How Fetch API and Async/Await Replace jQuery.ajax()

Still using jQuery for AJAX? Modern JavaScript Fetch API with async/await is cleaner, faster, and built-in. Modern Fetch vs Old jQuery Comparison: // OLD jQuery way $.ajax({ url: ‘/api/users’, method: ‘GET’, dataType: ‘json’, success: function(data) { console.log(data); }, error: function(xhr, status, error) { console.error(error); } }); // MODERN Fetch API async function getUsers() { try […]

Read More
Ajax

AJAX: Use Axios Interceptors to Automatically Retry Failed Requests

- 03.02.26 - ErcanOPAK comment on AJAX: Use Axios Interceptors to Automatically Retry Failed Requests

Network hiccups causing failed API requests? Axios interceptors can automatically retry failed requests before showing errors to users. Install Axios: npm install axios The Basic Retry Interceptor: import axios from ‘axios’; // Configure retry axios.interceptors.response.use( response => response, // Success – return as is async error => { const config = error.config; // If no […]

Read More
Ajax / JavaScript

Handle AJAX Errors Properly: Retry Logic with Exponential Backoff

- 01.02.26 | 01.02.26 - ErcanOPAK comment on Handle AJAX Errors Properly: Retry Logic with Exponential Backoff

Your AJAX calls fail randomly due to network hiccups or server load? Implement smart retry logic that actually works. The Problem with Basic Retry: // Bad: Immediate retry can make things worse function badRetry(url, maxAttempts) { for (let i = 0; i < maxAttempts; i++) { try { return await fetch(url); } catch (error) { […]

Read More
Ajax

Why Your Ajax Requests “Randomly” Fail in Production

- 31.01.26 - ErcanOPAK comment on Why Your Ajax Requests “Randomly” Fail in Production

Locally works.Production fails. Cause Preflight CORS requests. What happens Browser sends: OPTIONS /api/data Before: POST /api/data If server doesn’t handle OPTIONS → fail. Minimal fix (ASP.NET example) app.UseCors(builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); Why this matters Ajax failures are often network policy, not JS bugs.

Read More
Ajax / JavaScript

Why AJAX Requests Randomly Fail Under Load

- 30.01.26 - ErcanOPAK comment on Why AJAX Requests Randomly Fail Under Load

Works locally. Fails in production. Hidden issue Browsers limit parallel connections per origin. Fix Batch requests Use request queues Prefer a single endpoint await Promise.allSettled(requests); Why Uncontrolled parallelism = dropped requests.

Read More
Ajax / JavaScript

Why Fetch Requests “Randomly” Hang (But Server Is Fine)

- 29.01.26 - ErcanOPAK comment on Why Fetch Requests “Randomly” Hang (But Server Is Fine)

AJAX requests stall… server logs show nothing. Hidden reason Browsers limit parallel connections per origin (usually 6). When it happens Long-polling Hanging fetch calls Images + API calls from same domain Fix Abort unused requests Use AbortController const controller = new AbortController(); fetch(url, { signal: controller.signal }); // later controller.abort();  

Read More
Ajax / JavaScript

Ajax — Always Set Timeouts

- 28.01.26 - ErcanOPAK comment on Ajax — Always Set Timeouts

$.ajax({ timeout: 5000 }); Why it mattersPrevents hanging UI states.

Read More
Ajax / JavaScript

Throttle Requests to Save Backend

- 27.01.26 - ErcanOPAK comment on Throttle Requests to Save Backend

let timeout; input.oninput = () => { clearTimeout(timeout); timeout = setTimeout(sendRequest, 300); }; Why it mattersLess load, better UX.

Read More
Ajax / JavaScript

Send JSON the Right Way

- 26.01.26 - ErcanOPAK comment on Send JSON the Right Way

fetch(url, { method: “POST”, headers: { “Content-Type”: “application/json” }, body: JSON.stringify(data) }); Why it mattersAvoids silent model binding failures.

Read More
Ajax / JavaScript

Abort Fetch Requests to Prevent UI Race Conditions

- 25.01.26 | 25.01.26 - ErcanOPAK comment on Abort Fetch Requests to Prevent UI Race Conditions

Multiple rapid requests = stale UI updates. const controller = new AbortController(); fetch(url, { signal: controller.signal }); controller.abort(); Why this mattersPrevents outdated responses from overwriting newer state.  

Read More
Ajax / JavaScript

Stop Using JSON for Everything — Sometimes HTML Is Faster

- 24.01.26 - ErcanOPAK comment on Stop Using JSON for Everything — Sometimes HTML Is Faster

Returning pre-rendered HTML via Ajax can outperform JSON → DOM rebuild. fetch(‘/comments’) .then(r => r.text()) .then(html => container.innerHTML = html); Why this worksLess JS work, fewer reflows, simpler rendering.

Read More
Ajax / JavaScript

Ajax Requests Succeed but UI Never Updates

- 23.01.26 - ErcanOPAK comment on Ajax Requests Succeed but UI Never Updates

Network OK, UI frozen. Why it happensState updated outside render cycle. Why it mattersUsers see stale data. Vital fix Centralize state updates.

Read More
Ajax

Ajax Responses Arrive Out of Order

- 22.01.26 - ErcanOPAK comment on Ajax Responses Arrive Out of Order

Old data overwrites new. Why it happensRequests return unpredictably. Why it mattersUI shows wrong state. Smart fixCancel previous requests.

Read More
Ajax

Ajax Requests Block UI Unexpectedly

- 21.01.26 - ErcanOPAK comment on Ajax Requests Block UI Unexpectedly

User thinks page froze. Why it happensSynchronous requests or heavy callbacks. Why it mattersBad UX perception. Smart fixAlways async + lightweight callbacks.

Read More
Ajax / JavaScript

Ajax Forms Submit Twice

- 18.01.26 - ErcanOPAK comment on Ajax Forms Submit Twice

Users click once. WhyButton not disabled during request. TipDisable button until response. button.disabled = true;  

Read More
Ajax / JavaScript

Ajax Requests Succeed but Data Is Stale

- 16.01.26 - ErcanOPAK comment on Ajax Requests Succeed but Data Is Stale

Correct response, old UI. WhyClient-side caching assumptions. TipExplicitly control cache headers.

Read More
Ajax / JavaScript

Ajax Calls Succeed but UI Feels Delayed

- 15.01.26 - ErcanOPAK comment on Ajax Calls Succeed but UI Feels Delayed

Data arrives, UI lags. WhyDOM updates are too frequent. TipBatch DOM updates after Ajax responses.

Read More
Ajax

Ajax Calls Work but Data Order Breaks

- 14.01.26 - ErcanOPAK comment on Ajax Calls Work but Data Order Breaks

Responses arrive out of sequence. WhyNetwork latency differences. TipAlways track request IDs.

Read More
Ajax / JavaScript

Ajax Requests Work but Drain Battery on Mobile

- 13.01.26 - ErcanOPAK comment on Ajax Requests Work but Drain Battery on Mobile

Data loads fine, battery suffers. WhyPolling instead of event-driven updates. Tip Use push or conditional polling.

Read More
Ajax / JavaScript

Ajax Calls Block UI Under Load

- 12.01.26 - ErcanOPAK comment on Ajax Calls Block UI Under Load

UI freezes briefly. WhyHeavy JSON parsing on main thread. Tip Offload parsing to Web Workers.

Read More
Ajax / JavaScript

Ajax Requests Randomly Fail on Mobile

- 11.01.26 - ErcanOPAK comment on Ajax Requests Randomly Fail on Mobile

Desktop fine. Mobile broken. WhyMobile networks aggressively drop idle connections. Fix Set shorter request timeouts.

Read More
Ajax / JavaScript

Ajax Requests Succeed but Data Is Outdated

- 07.01.26 - ErcanOPAK comment on Ajax Requests Succeed but Data Is Outdated

Server updated, client not. WhyAggressive caching on GET requests. Fix Add cache-busting headers or query tokens.

Read More
Ajax / JavaScript

Ajax Calls Fail Only Behind Load Balancer

- 06.01.26 - ErcanOPAK comment on Ajax Calls Fail Only Behind Load Balancer

Direct calls succeed. WhyMissing forwarded headers. FixForward X-Forwarded-Proto.

Read More
Ajax / JavaScript

Ajax Requests Randomly Return Empty Responses

- 05.01.26 - ErcanOPAK comment on Ajax Requests Randomly Return Empty Responses

No errors, no data. WhyServer closes connection before body flush. FixExplicitly flush response on server side.

Read More
Ajax / JavaScript

Ajax Calls Fail When Tab Is Backgrounded

- 04.01.26 - ErcanOPAK comment on Ajax Calls Fail When Tab Is Backgrounded

Works when tab is active. WhyBrowser throttles background requests. FixUse Background Sync or retry logic.

Read More
Ajax / JavaScript

Ajax Works Locally but Fails in Production

- 03.01.26 - ErcanOPAK comment on Ajax Works Locally but Fails in Production

No errors, no data. WhyCORS preflight blocked. FixAllow OPTIONS requests on server.

Read More
Ajax / JavaScript

Ajax Requests Timeout Only in Production

- 02.01.26 - ErcanOPAK comment on Ajax Requests Timeout Only in Production

Local works, prod fails. WhyReverse proxy timeout limits. Fix Increase server timeout or chunk responses.

Read More
Ajax / JavaScript

AJAX Requests Work Locally But Fail in Production

- 01.01.26 - ErcanOPAK comment on AJAX Requests Work Locally But Fail in Production

No errors, no data. Why it happensCORS preflight blocked by server config. FixAllow OPTIONS requests explicitly.

Read More
Page 1 of 2
1 2 Next »

Posts navigation

Older posts
February 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
232425262728  
« Jan    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (934)
  • How to add default value for Entity Framework migrations for DateTime and Bool (830)
  • Get the First and Last Word from a String or Sentence in SQL (822)
  • How to select distinct rows in a datatable in C# (799)
  • How to make theater mode the default for Youtube (708)
  • Add Constraint to SQL Table to ensure email contains @ (572)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (552)
  • Average of all values in a column that are not zero in SQL (517)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (473)
  • Find numbers with more than two decimal places in SQL (436)

Recent Posts

  • C#: Use init Accessor to Create Immutable Objects Without Constructor Boilerplate
  • C#: Use Index and Range Operators for Cleaner Array Slicing
  • C#: Use Null-Coalescing Assignment to Simplify Lazy Initialization
  • SQL: Use CHAR Instead of VARCHAR for Fixed-Length Columns to Save Space
  • SQL: Use CROSS APPLY Instead of Subqueries for Better Performance
  • .NET Core: Use Required Modifier to Force Property Initialization
  • .NET Core: Use Global Using Directives to Avoid Repeating Imports
  • Git: Use git restore to Unstage Files Without Losing Changes
  • Git: Use git bisect to Find Which Commit Introduced a Bug
  • AJAX: Use Fetch with Signal to Cancel Requests When User Navigates Away

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (934)
  • How to add default value for Entity Framework migrations for DateTime and Bool (830)
  • Get the First and Last Word from a String or Sentence in SQL (822)
  • How to select distinct rows in a datatable in C# (799)
  • How to make theater mode the default for Youtube (708)

Recent Posts

  • C#: Use init Accessor to Create Immutable Objects Without Constructor Boilerplate
  • C#: Use Index and Range Operators for Cleaner Array Slicing
  • C#: Use Null-Coalescing Assignment to Simplify Lazy Initialization
  • SQL: Use CHAR Instead of VARCHAR for Fixed-Length Columns to Save Space
  • SQL: Use CROSS APPLY Instead of Subqueries for Better Performance

Social

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