Soy un codificador novato. Estoy tratando de insertar el var cPrice en mi página HTML de front-end. Sin embargo, no está definido y no puedo entender por qué.
///////////// Display Pop-up finciton ////////////////// //Start// $('#button1').on('click', function() { // Show the popup $('#openPopup').show(); // Get the symbol and name and show it in the popup const symbolID = document.getElementById("symbolSearch").value.toUpperCase(); document.getElementById("currentSymbol").innerHTML = symbolID; $.get("http://localhost:5000/placeOrder", function(err, price) { console.log(price.currentPrice); var cPrice = price.currentPrice // On the server-side it prints the correct value. console.log(cPrice) // However, here the front-end value is undefined. if (err) { console.log(err) } else { document.getElementById("currPrice").innerHTML = cPrice } }); // Execute the function for retrieving the price //userAction();Lea los documentos: jQuery.get() para la devolución de llamada de la función de success :
el primer argumento son los datos de PlainObject
jQuery.get( url [, data ] [, success ] [, dataType ] )
éxito fn
Tipo: función ( datos , estado de texto, jqXHR)
Una función de devolución de llamada que se ejecuta si la solicitud tiene éxito. Obligatorio si se proporciona dataType, pero puede usar null o jQuery.noop como marcador de posición.
También hay un
Aviso de desaprobación :
Los métodos de devolución de llamada jqXHR.success(), jqXHR.error() y jqXHR.complete() se eliminan a partir de jQuery 3.0 . Puede usar jqXHR.done(), jqXHR.fail() y jqXHR.always() en su lugar.
por lo tanto, será mejor que vaya con un código más legible usando .done() y .fail() :
$.get("http://localhost:5000/placeOrder") .done((data) => { console.log(data); console.log(data.currentPrice); }) .fail((jqXHR, textStatus, errorThrown) => { console.log(jqXHR); });