Answer the question
In order to leave comments, you need to log in
How to get data from ajax and display a specific value on the page?
I send a request via API to the server, like, the response comes in the form
{
service: "opt5",
online: 128,
total: 228,
forTotal: 10,
forOnline: 20,
country: "RU"
}
I need from this request as- then get only the value of the online parameter and display it,
PS There are a lot of services, they all have id and value with an individual code, so if you can somehow modify it so that the ajax request is sent in a cycle where the value of the service will change, and then sent to div with the same id, it would be great
Answer the question
In order to leave comments, you need to log in
You have some answer, if it is JSON, then it must first be parsed using JSON.parse and turned into data that JS can work with. After that, the trick is small, if it is an object, then you access the value directly: "response.online" or "response['online']". If this is an array, then iteration + here is an approximate solution to your problem with sending data to divs by ID:
const response = [{}, {}, {}];
response.forEach((item, idx) => {
const div = document.getElementById(`div-${idx}`);
div.innerHTML = item.online;
})
$.ajax({
url: '/ajax.php',
method: 'post',
dataType: 'json',
data: {
type: 'check_code',
code: code,
phone: phone
},
success: function (data) {
console.log(data);//тут объект
console.log(data.online);//тут свойство объекта(то, что вам нужно)
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question