我有 application.php 页面,其中 json 数组与 php 相呼应。在客户端上,我想在提交表单(submitHandler)时单击按钮获取带有 $.getJSON() 的 json 数组。问题是,如果我在 $.getJSON() 中运行 alert() 或 console.log() 什么也不会发生(我只在控制台中看到 GET 执行)。
代码:
$.getJSON('../views/application.php', function(data) {
alert('alert1');
if(data) {
document.write(data.resp);
alert('alert2');
}
else {
alert('error');
}
});
GET http://localhost/app/views/application.php
最佳答案
您可以在脚本中使用 var temp 并将数据存储在该变量中。
var x;
<script type="text/javascript">
x='<?php echo $comparisonResult; ?>';
</script>
关于javascript - 使用 $.getJSON() 从 php 获取 json 数组到 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21452968/