Dealing with Lengthy Requests: Effective Strategies

My ajax request sometimes takes a while to return a response.

  $.ajax({
    type: 'POST',
    url: 'some_url',
    data: data,
    processData: false,
    contentType: false,
    headers: {
      "Authorization": 'Token token="'+some_token+'"'
    }
  }).then(function(){
    do_something();
  });

Typically, if the request takes a few minutes, it works fine. However, if it exceeds around 10 minutes, I encounter the following error:

jquery.js:9175 POST 'some_url' net::ERR_EMPTY_RESPONSE

Even though the server continues to process my request and eventually sends back a Completed 201 Created ... response once finished, since the error occurs, there seems to be no listener for it at that point.

I would like to inform the user that the process has been completed.

Does anyone have suggestions for the best approach to handle this situation?

Your help would be highly appreciated.

Answer №1

When working with Ajax, I find it beneficial to use promises for handling asynchronous operations. Here's a simple example:

function send_ajax(url, data, some_token) {
    var ajax = $.ajax({
        type : 'POST',
        url : url,
        data : data,
        processData : false,
        contentType : false,
        headers : {
            "Authorization" : 'Token token="' + some_token + '"'
        }
    })

    return ajax.promise();
}


send_ajax("url","data","some_token").done(function(result) {
    console.log(result);
    do_something();
});

The promise allows the function inside of done to execute once the result is returned. It's worth noting that waiting 10 minutes for a response can be quite long.

edit: not the solution It has been pointed out that the original Ajax call remains within its own context, meaning the then() method should handle the returned call. The approach I initially suggested may not be applicable to the original problem at hand. For future reference, handling asynchronous tasks in a separate function with a returned value can help overcome such hurdles.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to dynamically create an object with a key and a value as a list in AngularJS

Hello, I have an object with the following structure: [{name: 'abc', country : 'US'},{name: 'xyz', country : 'IN'},{name: 'mno', country : 'US'},{name: 'pqr', country : 'IN'}] ...

Can we leverage protractor's by.cssContainingText for dynamic text conditions?

I am looking for a way to conditionally change text content in my web project, but I'm facing a challenge since the conventional methods seem limited. The cssContainingText function typically accepts string or regular expression values. Is there a wor ...

What is the method for obtaining the image alt attribute using JavaScript?

I can't seem to retrieve the image alt tag using JavaScript. Is it even possible and if so, how should I go about it? Perhaps something like this: $caption.text( flkty.selectedElement ).attr('alt') (last line of JavaScript) But I'm st ...

Endless Loop of Http Redirects in Node.js with Express

I need assistance with the code below which is meant to redirect all http traffic to https. // Implement redirect logic to ensure usage of https in production, staging, and development environments app.use((req, res, next) => { // Do not redirect to h ...

Dispense CSS using a React element

My React component index.js contains styling in style.css. I am looking to share this component on npm, but I am unsure of how to simplify the process for other developers to use the styling. After exploring different options, I have come across three ap ...

Issue encountered where Moment locale functionality is working in local development environment, but not functioning properly in the

My application built with Next.js requires displaying the date in Bengali instead of English. The issue arises after running the build command 'build:next build' where the production version displays the date in English (e.g. '20 January, Su ...

Tips for initiating and terminating the evaluation of a condition at regular intervals using JavaScript

I'm currently working on a JavaScript application where I need to achieve the following: Periodically check every 5 seconds to see if there is an element with the 'video' tag on the page. Once an element with the 'video' tag ...

Tips for managing onClick events within a conditional component

I am currently attempting to implement an onClick event on the data that I have selected from the AsyncTypeahead. My goal is to pass a callback function to the Problem component, which will only render if an item has been selected. However, after selecting ...

Error! React is unable to find the window object

I recently added the "react-speech" package to my application in order to incorporate text-to-speech functionality. However, upon importing the package, I encountered an error that has been challenging to resolve despite extensive research. Any assistance ...

Isotope data-filter not working properly following an Ajax callback

I'm looking for a way to create a filter that can be dynamically updated: I have utilized the isotope javascript library in an external script file: var $container = $('.isotope'); // initialize isotope $container.isotope({ ...

Showing a JSON array with varying nested lengths in an HTML5 format

I have a JSON string containing nested arrays with elements of unequal lengths. I am looking to display this data on a page using either Javascript or Angular.js. { [ 'id':1, 'value':'tes1' 'nextLevel':[&ap ...

Could implementing a click/keydown listener on each cell in a large React datagrid with thousands of cells impact performance?

Years ago, before the advent of React, I mastered linking events to tables by attaching the listener to the <tbody> and extracting the true source of the event from the event target. This method allowed for a single listener for the entire table, as ...

The proper way to compare numbers in JavaScript

I need to determine the color for my legend by comparing two values without using ceil, floor, or round functions. The color is based on a color table with point and RGB values. The backend provides points like 0.4607441262895224, 0.5500956769649571, etc. ...

Creating a duplicate object in a React component

I am currently developing a basic react application with inputs such as first name, last name, etc... The input states are managed using context api in the following context.js: import React, { useState } from "react"; export const FormContext ...

Creating a customized image modal in ReactJS that incorporates a dynamic slideshow feature using the

I am attempting to incorporate an image lightbox into my React application: https://www.w3schools.com/howto/howto_js_lightbox.asp Here is the link to the CodeSandbox where I have tried implementing it: https://codesandbox.io/s/reactjs-practice-vbxwt ...

jquery did not load properly in the Google Chrome extension

Why am I unable to use a jQuery script inside my "myscript.js" file in the following Google Chrome extension? Is jQuery not loaded within the "myscript.js" file? What changes need to be made in the manifest file to enable the usage of jQuery inside "myscri ...

Angular is throwing an error stating that the type '{ }[]' cannot be assigned to the type '[{ }]'

I'm in need of assistance and clarification regarding the error I encountered in my application... When I receive a JSON response from an API with some data that includes an array of products, I aim to extract these products (izdelki) from the array, ...

Managing all AJAX success events in one centralized location using jQuery

In a particular situation, I find myself needing to handle all jquery success events in one centralized location. This is because I want a specific function to be called after every ajax success event occurs. While I am aware that I can use $.ajaxComplete ...

Populate a table dynamically using JavaScript based on existing cell values

I am currently working on filling an HTML table based on the contents of its left cells. The code below creates a table that is populated with variables: <table id="myTable" ></table> <script> var rightcell = [{name : "Name ...

Issue occurring while trying to select an item from the dynamically generated options using AJAX

A JavaScript function is used in this code to select a specific option, with the option value being specified within a hidden element: $("select").each(function() { var id = $(this).attr('id'); var source = 'input:hidden[na ...