-1
Hello, I am trying to change a text on my web page to follow in real time some insertion in the database, I am using synchronous AJAX to make the requests, but HTML does not change.
Javascript:
result.forEach(function (value) {
$.ajax({
url: Config.path+"/admin/sensor/?machineId=" + machineId,
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
data: JSON.stringify({name:value.name, collectType:value.collectType, nameAddress:value.nameAddress, ihmOperator:value.ihmOperator, dataType: value.dataType,
monitoring: value.monitoring, analytics: value.analytics, setpoint: value.setpoint, notification: value.notification,
class_type:value.class_type, range:value.range, charbehauvior:value.charbehauvior, observations:value.observations, showSensor:value.showSensor}),
success : function(data, success, response){
switch(response.status){
case 200:
$('#sensorName').text(value.name);
console.log(data);
}
}
});
});
HTML:
<div class="modal-footer">
<p id="sensorName" style="text-align: left;" class="hidden"></p>
</div>
"console.log(data)", just below the command to change the HTML, works, prints what I want on the console. Can anyone help me? Thanks.
Then use $('#sensorName'). text(data), just be careful because the callback Success "date" parameter is usually an Array. If a password is an array with its date variable of the callback.
– Ricardo Lucas
But if I put console.log(value.name), it prints on the console as expected, I think this problem has something to do with AJAX being synchronous, because it only prints the last record being inserted, but I need it to be synchronous.
– João Pedro Oliveira Souza
The "date" variable when you give a console.log(data), does it have the values you want ? Do you want to put only 1 value in the variable or put all values that come from the date variable on the screen? try to be clearer so I can help you.
– Ricardo Lucas
I’m just trying to put the date name variable, which is the same thing as value.name. When I give the console.log(data) it prints the correct values, and when I print console.log(value.name) as well. 'data' and 'value' are equal.
– João Pedro Oliveira Souza
Put an image of your.log(data) console to see what’s on it. So you have to put all the values of the Array data in a string and show in the variable "sensorName" is this ?
– Ricardo Lucas
No, inside the data array, it has the name variable, it’s what I’m trying to put. The data.name. The value I put there is equal to date, data = value and data.name = value.name
– João Pedro Oliveira Souza
I think your value may be being overwritten after all you are inside a foreach, and the answers given should help, can you put a photo of the log ? or something like that to see if you can help .
– Ricardo Lucas