我想将ajax响应用作全局变量。不能在0f成功声明之外使用。所以,
这是我的代码:
<script>
var id = $('#@Model.Id').val();
var connectionData;
$.ajax({
type: "POST",
url: '@CustomUrl.SendConnectionTime(@Model.Id)',
data: id,
success: function(response2) {
connectionData = response2;
},
error: function () {
alertify.set('notifier', 'position', 'bottom-right');
alertify.error("Veri göndermede hata oluştu.");
}
});
</script>
我想在另一个组件中使用connectionData:
var myChart = new Chart(canvas,
{
type: 'line',
data: {
labels: times,
datasets: [
{
label: dataConnectionTitle,
data: connectionData,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true
}
}
]
}
}
}
);
},
error: function() {
alertify.set('notifier', 'position', 'bottom-right');
alertify.error("Veri göndermede hata oluştu.");
}
});
但是当我调试connectionData时似乎未定义。