The response from Ajax appears to be unclear

I'm attempting to assign the ajax response to a variable (let's call it _has_weekend_hollidays) in the following manner:


JS Call:

_has_weekend_hollidays = checkWeekendHollidays( _i );
console.log ( checkWeekendHollidays( _i ) );

AJAX Call

$.ajax({
    url: "./directory/ajax_check_weekend_hollidays.php",
    type: "POST",
    data: { 
            start_date: _date1,
            final_date: _date2 
          }
}).done(function (_result) {
    return ( _first_weekday == 0 || _last_weekday_id >= 6 || _result!="0" );
}).fail(function (_result) {
    console.log("ERROR:" + _resultado);
});

The AJAX response is fine but the return statement is not functioning.

Therefore, console.log displays undefined.

Any suggestions?

Thanks in advance!

Answer №1

If you want to ensure the result is processed correctly, make sure to perform your tasks after the Ajax callback has completed.

The reason it returns as undefined is because no variables are returned by the Ajax request:

{
    url: "./directory/ajax_check_weekend_hollidays.php",
    type: "POST",
    data: { 
            start_date: _date1,
            final_date: _date2 
          }
}

This code snippet:

return ( _first_weekday == 0 || _last_weekday_id >= 6 || _result!="0" );

will not be executed until the Ajax call is marked as "Done".

If you want to track the returned data, utilize console.log within the return function.

For example:

$.ajax({
    url: "./directory/ajax_check_weekend_hollidays.php",
    type: "POST",
    data: { 
            start_date: _date1,
            final_date: _date2 
          }
}).done(function (_result) {
    console.log(_first_weekday == 0 || _last_weekday_id >= 6 || _result!="0");
    //additional actions can be performed here
}).fail(function (_result) {
    console.log("ERROR:" + _resultado);
});

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

Unique phrase: "Personalized text emphasized by a patterned backdrop

I'm facing a challenge and struggling to find a way to highlight text using CSS or jQuery. My goal is to have an image on the left, another one on the right, and a repeated image in between. Since there are some long words involved, I need a dynamic s ...

Reorganizing array elements to match the order of another array in ES6

I am attempting to organize one array based on the order of another array... For instance... (Utilizing ES6 React) const orderArr = ["Daniel","Lucas","Gwen","Henry","Jasper"]; const nameArr = ["Gwen","Jasper","Daniel"]; in order to output Daniel // fi ...

Using PHP to fill input field with text output

I've been searching for an answer to this question, but so far I haven't found one. In my database, I input two pieces of data and receive a success or failure outcome. What I want is to have a "Submit" button that triggers my PHP query (which ...

Implementing Pagination or Infinite Scroll to Instagram Feed

Currently, I am working on creating an Instagram feed for a fashion campaign where users can hashtag their photos with a specific tag. Using the Instagram API, the script will pull all recent posts with this common tag to display on the webpage. Instagram ...

Can someone help me troubleshoot the issue with my submit button's onclick function?

I am currently working on a project where I have a content box enclosed in a div tag. Within this content box, there are paragraphs with unique IDs for individual styling rules. I have set margins, padding, and other styles accordingly. At the bottom of th ...

Tips for embedding a script into an HTML document

I've been experimenting with tinymce npm and following their guide, but I've hit a roadblock. Including this line of code in the <head> of your HTML page is crucial: <script src="/path/to/tinymce.min.js"></script>. So, I place ...

Encountering 404 errors on dynamic routes following deployment in Next.JS

In my implementation of a Next JS app, I am fetching data from Sanity to generate dynamic routes as shown below: export const getStaticPaths = async () => { const res = await client.fetch(`*[_type in ["work"] ]`); const data = await re ...

Retrieve the nearest identifier text from the tables

I have a table on my webpage with some data: <tbody id="carga"> <tr> <td>1</td> <td id="nombre">esteban</td> <td id="apellido">aguirre</td> <td>N/A</td> <td>N/A</td ...

The attempt to run 'setProperty' on 'CSSStyleDeclaration' was unsuccessful as these styles are precalculated, rendering the 'opacity' property unchangeable

I am attempting to change the value of a property in my pseudo element CSS class using a JavaScript file. Unfortunately, I keep encountering the error mentioned in the title. Is there any other method that can be used to achieve this? CSS Code: .list { ...

Label alignment in one line with responsive checkbox

Could someone assist me with adjusting the alignment of this checkbox when resizing the window for mobile view? The label text is breaking into a new line while the checkbox remains in its original position. How can I make the checkbox align closer to its ...

Having difficulty breaking down values from an object

Attempting to destructure the data object using Next.js on the client side Upon logging the data object, I receive the following: requestId: '1660672989767.IZxP9g', confidence: {…}, meta: {…}, visitorFound: true, visitorId: 'X9uY7PQTANO ...

The final thumbnail fails to appear in the visible display (react-responsive-carousel)

I am currently facing an issue with displaying a series of images using react-responsive-carousel. When the images exceed a certain size causing the thumbnail section to become scrollable, the last thumbnail is always out of view. Although I have impleme ...

Flask does not provide a direct boolean value for checkboxes

After struggling for a week, I am still lost on where to make changes in my code. I need the checkbox to return a boolean value in my Flask application. Below are snippets of the relevant code: mycode.py import os, sqlite3 from flask import Flask, flash ...

What is the best method for storing and retrieving data retrieved from an HTTP response?

Utilizing the request module, I am making a call to an API that sends me a Base64 encoded response in the form of a file. app.get("/report", async(request, response) => { const newReq = new mdl.Request const newSources = new mdl.Datasource ...

Having trouble with Node integration in Electron?

In my inventory application, I have the backend written in Python 3.7 and I am using Electron to create a GUI for it. To communicate with the Python code, I am utilizing the Node.js Module "python-shell" and would like to keep all of its code in a separate ...

When attempting to embed Ruby code within JavaScript, Ruby is not being acknowledged or

I am facing an issue with accessing values of a hash created by a ruby function inside javascript. This is the code snippet from my controller: class TransferController < ApplicationController def index require 'json' #@t ...

Utilizing a combination of a `for` loop and `setInterval

I've been encountering an issue for the past 3-4 hours and have sought solutions in various places like here, here, here, etc... However, I am unable to get it to work in my specific case: var timer_slideshow = {}; var that, that_boss, has_auto, el ...

Steps to retrieve the latest value of a specific cell within the Material UI Data Grid

After updating the cell within the data grid, I encountered an issue where I could retrieve the ID and field using the prop selectedCellParams, but retrieving the modified value was proving to be challenging. In order to successfully execute the PUT reque ...

The issue of memory leakage with ng-grid and real-time data

My intention is to utilize ng-grid for visualizing high-frequency real-time data, but I am encountering issues with a memory leak. Interestingly, the memory leak does not occur when I opt for a simple HTML table with ng-repeat. My tech stack includes node ...

How can I adjust the Line Opacity settings in Google Charts?

Within my Google Charts project, I have successfully implemented a feature that changes the color of a line halfway through a graph based on certain conditions using a dataView. Here is the code snippet demonstrating this functionality: var dataView = new ...