The value passed to the JavaScript function appears to be null, despite not actually being null

Here are the specific API functions I am using to call from JavaScript:

function SuspendActionOp(barcode, owner) {
//alert(barcode+" - "+num);
alert(owner);
if (owner == null) {
    alert('owner is null');
} else {
    $.ajax({
        url: 'http://myserver/myapp/api/Actions?action=2&barcode=' + barcode + '&owner=' + owner,
        //data: { id: barcode },
        dataType: 'json',
        type: 'post',
        success: function (a) {
            //var t = JSON.parse(a);
            if (a.result.localeCompare("suspended") == 0) {
                $("#details_" + barcode).modal("hide");
                setTimeout(MachinesBigOp(), 2000);
            } else {
                alert("Error");
            }
        }
    });

}
}

This function is called with a value for owner:

function ActionsOp(stato, barcode, num, owner) {
alert('ActionsOp '+owner);
var btngroup = document.createElement('div');
btngroup.classList.add("btn-group");

switch (stato) {

    case 1:
        var s = document.createElement("a");            
        s.onclick = function () {
            //var t = owner;
            alert(owner);
            SuspendActionOp(barcode, num, owner);
        };
        s.classList.add("btn");
        s.classList.add("btn-warning");
        s.innerHTML = "❚❚";

        btngroup.appendChild(s);
        var e = document.createElement("a");

        e.classList.add("btn");
        e.classList.add("btn-danger");
        e.innerHTML = "✔";
        e.onclick = function () {
            this.parentNode.parentNode.appendChild(ModalEndOp(barcode, owner));
            $('#end_' + barcode).modal({ backdrop: false });
        };
        e.style.marginLeft = "40px";
        btngroup.appendChild(e);
        break;
    case 2:
        var a = document.createElement("a");

        a.onclick = function () {
            this.parentNode.parentNode.appendChild(ModalStartOp(barcode, owner));
            $("#start_" + barcode).modal({ backdrop: false });
        };
        a.classList.add("btn");
        a.classList.add("btn-success");
        a.innerHTML = "⚡";
        btngroup.appendChild(a);
        break;
    case 4:
        break;
}
return btngroup;
}

from:

function ModalDetailsOp(num, owner) {
    alert('ModalDetailsOp' + owner);  
   //.........
   var azioni = document.createElement('td');
   azioni.appendChild(ActionsOp(a[i].Stato, a[i].Barcode, a[i].NumMachine, owner));
    //.........
}

from:

    function MachinesBigOp(owner) {
        alert('MachinesBigOp'+owner);
        $.ajax({
            url: 'GetMachines',
            data: {},
            dataType: 'json',
            type: 'get',
            success: function (a) {

                var cont = document.getElementById("machineContainer");
                cont.innerHTML = "";
                var l = a.length;
                var i = 0;
                for (i = 0; i < l; i++) {
                    var m = document.createElement("a");
                    m.classList.add('machine');
                    m.classList.add('hover-shadow');
                    m.classList.add('show');
                    m.style.color = "black";
                    cont.appendChild(ModalDetailsOp(a[i].NumMachine,owner));
//.....
}

and finally from html razor page:

<script>
    $(function () {
        filterSelection("all");
        filterInit();
        MachinesBigOp('@Session["id"].ToString()');
    });

</script>

When passing the owner variable with its value and calling SuspendActionOp(), the owner value appears to be null. I am struggling to identify the cause of this issue. Any help would be greatly appreciated.

Answer №1

You have defined your SuspendActionOp function with 2 parameters.

SuspendActionOp(barcode, owner)

However, when you are calling it from the ActionsOp, you are passing in 3 parameters:

SuspendActionOp(barcode, num, owner);

As a result, in this scenario, the num parameter will actually hold the value of owner!

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

Sending JSON data stored in a JavaScript variable through a jQuery POST request

I am currently attempting to retrieve data from a remote location using a JQuery post method. It works perfectly fine when I directly input the data to be posted, but for some reason, it fails when I store the JSON in a JavaScript variable and pass it in. ...

Identifying Shared Links in Node.js

Is there a way to determine if a file-system path is a hard link using Node.js? The fs.lstat function provides a stats object that can identify if a path is a hard link by returning true for stats.isDirectory() and stats.isFile() respectively. However, the ...

The filtering and sorting features of Ng-table do not seem to be working properly when dealing with grouped data

Currently, I am in the process of learning angular.js and have recently started using the ng-table directive. While I have successfully managed to group my data and display it using ng-table, I seem to be facing issues with sorting and filtering the data. ...

Having trouble locating the module 'monaco-editor/esm/vs/editor/editor.worker' while using create react app

I am currently facing some challenges running react-monaco-editor in my project despite following the documentation and making necessary adjustments to my files. Below is a snippet of my package.json file: { "name": "chatbot_compiler", "version": "0. ...

What is the method to obtain the dimensions of the browser window using JavaScript?

In my JavaScript code, I am trying to retrieve the browser window size and then set the size of a div element accordingly. I have two div elements that should adjust based on the window size. For example, if the window size is 568px, div1 should be 38% of ...

Ruby on Rails: Making an Easy AJAX Call

i am encountering an issue with my ajax request: within my application, i am attempting to set up a basic ranking system. Once configured, whenever I click on the rank button, the page reloads and the rank is refreshed. I need help understanding how to i ...

Looking to improve query performance on MongoDB using mongoskin - should you use a single query or

this relates to mongodb {cod_com:'WWWOAN', cod_prod[{prod:'proda',info:'hello world'},{prod:'pacda',info:'hello world'},{prod:'prcdb',info:'hello world'}] } {cod_com:'WWWOA2&a ...

JavaScript AJAX Event Listener

Currently seeking a way to intercept ajax events in JavaScript before any Ajax method is triggered. While there is an ajaxListener in JQuery that could work if all the ajax requests were from JQuery, unfortunately, they are not. So the question remains: ho ...

Obtaining data from a dropdown list and populating a textbox with the selected

As a novice in the world of PHP, I am trying to achieve a result in a text field based on the selection made in a dropdown list. My main challenge lies in extracting and displaying the value in the text field. Despite my extensive search for a solution, I ...

What is the process of transferring information to a property in JSON within a Jade (Pug) file?

Initially, I transmit data to a Jade template using Node.js. app.get('/', function(req, res){ var arr = new Array( {firstname: 'Gil-dong', lastname: 'Hong'}, {firstname: 'Yeong-sil', lastname: &a ...

What are some ways I can customize the appearance of this Google Maps infoWindow?

I was able to create a Google Maps script using JavaScript code. The map displays multiple locations with corresponding latitude and longitude coordinates. This script can be viewed at . My objective now is to customize the appearance of the info windows ...

Is it possible for the versions in the package.json file to be altered

I've made updates to my package.json file - all packages are listed as follows: "dependencies": { "@apollo/client": "3.6.4", "bootstrap": "4.6.2", "graphql": "16.5.0" } ...

When the user clicks, I plan to switch the audio source

I am looking to update the audio source when a button is clicked, but I am having trouble getting it to work. image description data() { return { audioSrc: '' } }, methods: { setActiveAudio(item) { this.$refs.audioE ...

Exploration of XML Parsing with Jquery

Is there a way to extract XML from this specific link? We're referring to: I have attempted the following code snippet with hopes of achieving success: $.ajax({ url: "http://clarendoncollegeactivities.blogspot.com/feeds/posts/default", dat ...

bespoke slickgrid dropdown editor

I am currently working on implementing a slickgrid feature where a cell needs to display a dropdown (selectlist) with values fetched from a restful service. These values will vary for each row. The requirement is that the user should be able to select one ...

What is the best way to create text boxes that change dynamically according to user input using PHP and JavaScript?

Is it possible to modify the code below to allow for dynamic dropdowns to appear based on a number entered by the user in a textbox? I would like the dropdowns to adjust based on the number provided by the user. $query = mysqli_query($con, "SELECT * FRO ...

Can you explain the significance of this regular expression?

I'm trying to decipher the meaning of this regular expression. Can anyone help? "^[A-Z]{3}-[4-7]\d{2,4}\$$" My understanding is that it must start with exactly 3 uppercase letters and end with a sequence of 2, 3, or 4 digits (although I a ...

having difficulty iterating through the data using map function

When attempting to use the map function in React Material UI to display an array of data in a select box, I encountered the error TypeError: Cannot read properties of undefined (reading 'map') while using the following code. Can anyone help me id ...

What is the significance of XmlHttpRequest's readyState being at 2 when receiving a 200 HTTP response

Currently, I am utilizing pure JavaScript (no jQuery) to send a file to the server. The PHP script on the server returns status code 200 upon completion, but the JavaScript is interpreting it as readyState == 2. The PHP code responds with status code 200: ...

Monitor modifications to the form as the user navigates away from the page or refreshes

Currently, I am working on a form that was created using Formik and React Hooks. I would greatly appreciate any suggestions on how to track changes made to the form when a user clicks the home button or refreshes/reloads the page. I attempted to use the b ...