我有以下PHP文件,将在我的JS中使用$.ajax()
调用该文件:
<?php
header('Content-Type: application/json');
echo json_encode(array('One' => 'Test'));
?>
我的JS中有以下代码:
<script type = "text/javascript">
let changeValue;
$('#someElementID').click(function() {
$.ajax({
type: 'POST',
url : 'phpfile.php',
dataType : 'json',
success: function(obj, textStatus) {
changeValue = obj.One;
}
});
$('#toBeChangedElementValue').text(changeValue);
}
</script>
问题是:changeValue
变量仅在执行我的$('#toBeChangedElementValue').text()
之后获得其值。它是如何工作的?我该如何解决?我必须在成功回调中放置$('#toBeChangedElementValue').text(changeValue)
才能使其正常工作。为什么我的changeValue
在最后的.text()
发生之前没有被首先更新?